Problem in Update & Delete

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
puvinax
Posts: 10
Joined: Mon 09 Sep 2013 15:43

Problem in Update & Delete

Post by puvinax » Wed 30 Oct 2013 08:19

Hi

I wrote a repository pattern for CRUD
insert , getAll works but when i use Delete and Update methods get errors



DELETE :

Code: Select all

public T Delete<T>(T t) where T : class
        {
            T obj;
            try
            {
                Set<T>().Remove(t);
                SaveChanges();
                obj = t;
            }
            catch (Exception)
            {
                obj = null;
            }
            return obj;
        }
DELETE ERROR :
The object cannot be deleted because it was not found in the ObjectStateManager.
When I change Delete Method to this error change

Code: Select all

public T Delete<T>(T t) where T : class
        {
            T obj;
            try
            {
                Set<T>().Attach(t); // Add THIS
                Set<T>().Remove(t);
                SaveChanges();
                obj = t;
            }
            catch (Exception)
            {
                obj = null;
            }
            return obj;
        }
NEW DELETE ERROR :
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

UPDATE :

Code: Select all

        public T Update<T>(T t) where T : class
        {
            T obj = null;
            try
            {
                var entry = Entry(t);
                Set<T>().Attach(t);
                entry.State = EntityState.Modified;
                SaveChanges();
                obj = t;
                return obj;

            }
            catch (Exception)
            {
                return obj;
            }

        }
UPDATE ERROR :
Connection must be opened.
USE METHODS :

Code: Select all

 var monitor = new Devart.Data.SQLite.SQLiteMonitor { IsActive = true };

            Devart.Data.SQLite.Entity.Configuration.SQLiteEntityProviderConfig config = Devart.Data.SQLite.Entity.Configuration.SQLiteEntityProviderConfig.Instance;
            config.Workarounds.IgnoreSchemaName = true;

            var leitner = new LeitnerBoxContext();
            leitner.Database.Initialize(false);

            var br = new BusinessRepo<Account>(leitner);
            var items = br.GetAll<Account>().ToList();

            var ent1 = br.Insert(new Account { UserName = "AAA", Password = "CCC", SecurityAnswer = "E", SecurityId = 1 });
            var ent2 = br.Insert(new Account { UserName = "SSS", Password = "DDD", SecurityAnswer = "F", SecurityId = 2 });
            
            // Update ent1
            var ent3 = br.Update(new Account { UserName = "TTT", Password = "GGG", SecurityAnswer = "E", SecurityId = 1 });

            // Delete ent2
            var ent4 = br.Delete(new Account { UserName = "SSS", Password = "DDD", SecurityAnswer = "F", SecurityId = 2 });

I hope anyone help me

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Re: Problem in Update & Delete

Post by Shalex » Thu 31 Oct 2013 16:48

We have checked the workability of the code generated by the DbContext and Repository And Unit of Work templates (Entity Developer). Please try this walkthrough with dotConenct for SQLite: http://forums.devart.com/viewtopic.php? ... 678#p85646.

If you add reference to EF v6 in your project, refer to this article to get an example of EF provider registration in app.config: http://blog.devart.com/entity-framework ... force.html (don't register migrationSqlGenerator via *.config, EFv6 doesn't support this scenario for some reason).

puvinax
Posts: 10
Joined: Mon 09 Sep 2013 15:43

Re: Problem in Update & Delete

Post by puvinax » Thu 31 Oct 2013 17:04

I do not know if this is true or not
Maybe I have a specific problem
It is more important for me to solve my problems based on my sample code
Can you look at it and try to solve the problems?
I know this is a shameful desires :oops:
I have new in repository pattern and in design simple crud framework. with basic knowledge I Created simple framework please see above sample and if anyone can and have time change in to correct way
I asked these questions in some popular programming forums but no one see my example or answer clearly

You are the last hope to solve my problems on my code example

Sample Code : https://skydrive.live.com/?cid=F75FCD99 ... F749C9!107

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Re: Problem in Update & Delete

Post by Shalex » Thu 07 Nov 2013 15:19

Sorry but this question exceeds the goals of our support.

Please try using the Repository And Unit of Work template which is shipped with Entity Developer.

Post Reply