Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't update a record using entity framework and ninject with Repository pattern
    primarykey
    data
    text
    <p>I am using MVC3, Ninject , Ninject MVC extensions, EF 4.1</p> <p>I have implemented the following repo pattern.</p> <p>Repository.cs</p> <pre><code>public interface IRepository&lt;T&gt; where T : class { IQueryable&lt;T&gt; AsQueryable(); IEnumerable&lt;T&gt; GetAll(); IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; where); T Single(Expression&lt;Func&lt;T, bool&gt;&gt; where); T First(Expression&lt;Func&lt;T, bool&gt;&gt; where); void Delete(T entity); void Add(T entity); void Attach(T entity); } public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class { IObjectSet&lt;T&gt; _objectSet; public Repository(IObjectContext objectContext) { _objectSet = objectContext.CreateObjectSet&lt;T&gt;(); //_objectSet.MergeOption = MergeOption.NoTracking; } public IQueryable&lt;T&gt; AsQueryable() { return _objectSet; } public IEnumerable&lt;T&gt; GetAll() { return _objectSet.ToList(); } public IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; where) { return _objectSet.Where(where); } public T Single(Expression&lt;Func&lt;T, bool&gt;&gt; where) { return _objectSet.Single(where); } public T First(Expression&lt;Func&lt;T, bool&gt;&gt; where) { return _objectSet.First(where); } public void Delete(T entity) { _objectSet.DeleteObject(entity); } public void Add(T entity) { _objectSet.AddObject(entity); } public void Attach(T entity) { _objectSet.Attach(entity); } } </code></pre> <p>Unit of Work.cs</p> <pre><code> public interface IUnitOfWork { void Commit(); } public class UnitOfWork : IUnitOfWork, IDisposable { private readonly IObjectContext _objectContext; public UnitOfWork(IObjectContext objectContext) { _objectContext = objectContext; } public void Dispose() { if (_objectContext != null) { _objectContext.Dispose(); } GC.SuppressFinalize(this); } public void Commit() { _objectContext.SaveChanges(); } } </code></pre> <p>ObjectContext.cs</p> <pre><code>public interface IObjectContext : IDisposable { IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() where T : class; void SaveChanges(); } public class ObjectContextAdapter : IObjectContext { readonly ObjectContext _context; public ObjectContextAdapter(ObjectContext context) { _context = context; } public void Dispose() { _context.Dispose(); } public IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() where T : class { return _context.CreateObjectSet&lt;T&gt;(); } public void SaveChanges() { _context.SaveChanges(); } } </code></pre> <p>I have used Ninject to bind them in the IOC using the following implementation</p> <pre><code> public override void Load() { Bind(typeof(IRepository&lt;&gt;)).To(typeof(Repository&lt;&gt;)); Bind&lt;IObjectContext&gt;().To&lt;ObjectContextAdapter&gt;().WithConstructorArgument("context", new MGTContainer()); Bind&lt;IUnitOfWork&gt;().To&lt;UnitOfWork&gt;(); } </code></pre> <p>The repo pattern is taken from here <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/" rel="nofollow">Repo Pattern Implementation</a></p> <p>Here is a sample use of the pattern in the controller</p> <pre><code> eventRepository.Attach(model); unitOfWork.Commit(); </code></pre> <p>All works fine for Inserting, Reading and Deleting records but when i call the attach i have the following error</p> <blockquote> <p>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.</p> </blockquote> <p>I have tried changing the binding to InRequestScope but this causes subsequent errors.</p> <p>Thanks in advance for your help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload