none
EF 4.0 POCO Adding entity: A duplicate value cannot be inserted into a unique index. RRS feed

  • Question

  • I got an issue that I have ben struggling with for the last 2 days and I’m about to give up.
    I’m using EF 4.0 POCO with a repository pattern, but when I tries to add en new entity to the database I get the following error: “A duplicate value cannot be inserted into a unique index. blah blah”.

    When I examine the objectstates I see that EF has marked my entity as added (which is correct), but it has also marked all the new entity’s  navigation properties (already existing in the database) as added which of course it why I get the exception when I tries to save the objectcontext.
    I’m guessing the problem relies in my repository code, but I can’t see why it’s not working:

    public class EntityRepository<T>: IRepository<T> where T : class
        {
            private ObjectContext _context;
            private ObjectSet<T> _objectSet;
            private ObjectQuery<T> _objectQuery;
    
            public EntityRepository(ObjectContext context,string[] includes)
            {
                _context = context;
                _context.ContextOptions.LazyLoadingEnabled = false;
                _context.ContextOptions.ProxyCreationEnabled = false;
    
                _objectSet = _context.CreateObjectSet<T>();
                _objectQuery = _objectSet;
                if(includes != null)
                    foreach (var include in includes)
                    {
                        _objectQuery = _objectQuery.Include(include);
                        _objectSet.Include(include);
                    }
            }
    
            public ObjectContext Context
            {
                get { return _context; }
            }
    
            
           
            public IQueryable<T> Fetch()
            {
    
                return _objectQuery;
            }
    
            public IEnumerable<T> GetAll()
            {
                   return Fetch().ToList();
            }
    
            public IEnumerable<T> Find(Func<T, bool> predicate)
            {
                return _objectQuery.Where<T>(predicate).ToList<T>();
            }
    
            public T Single(Func<T, bool> predicate)
            {
                    return _objectQuery.Single<T>(predicate);
            }
    
            public T First(Func<T, bool> predicate)
            {
           
                    return _objectQuery.First<T>(predicate);
            }
    
            public void Delete(T entity)
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }
                _objectSet.DeleteObject(entity);
            }
    
            public void Delete(Func<T, bool> predicate)
            {
                IEnumerable<T> records = from x in _objectQuery.Where<T>(predicate) select x;
                foreach (T record in records)
                {
                    _objectSet.DeleteObject(record);
                }
            }
    
            public void Add(T entity)
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }
                
                _objectSet.AddObject(entity);
            }
    
    
            public void Attach(T entity)
            {
                _objectSet.Attach(entity);
            }
    
            public void SaveChanges()
            {
                _context.SaveChanges();
            }
    
            public void SaveChanges(SaveOptions options)
            {
                _context.SaveChanges(options);
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
    
            protected virtual void Dispose(bool disposing)
            {
    
                if (disposing)
                {
                    if (_context != null)
                    {
                        _context.Dispose();
                        _context = null;
                    }
                }
    
            }
        }

    My best guess is that the error is in the way I load the navigation properties, but I can’t figure out how to fix it 

    Any help is welcome.


     /Mikkel Snitker

    Monday, March 26, 2012 8:30 AM

All replies

  • Hi Mikkel Snitker,

    Welcome to MSDN Forum.

    Could you please show the code how do you insert the record?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, March 28, 2012 2:06 AM
    Moderator
  • Hi Allen Li.

    I had a deadline which unfortunately resulted in I had to skip selftracking entities
    and go back to plain POCO with a single instance ObjectContext for the entire
    application. I have created a new thread where I seek advice for building the
    application the “proper” way. So if you got the time I would appreciate any
    advice.

    Link to the
    new thread: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/1b8cbf64-f70d-4d49-9b65-ed4423610a74

    /Mikkel


    Wednesday, March 28, 2012 7:53 AM