In EF, DbContext and DbSet, generally speaking, implement UnitOfWork and Repository out of the box, respectively. On the Internet, there are thousands of examples of how people, following clearly in alphabetical order, wrap their hands in their classes, which implement their interfaces. Something like this:
public interface IUnitOfWork : IDisposable { IRepository<T> GetRepository<T>() where T : class; void SaveAllChanges(); } public interface IRepository<T> : IDisposable where T : class { IQueryable<T> Entities(); void Update(T entity); void Add(T entity); void Remove(T entity); bool Contains(T entity); } It turns out a kind of abstraction over abstraction, in many places they write that this should not be done, but nowhere is an example of how to implement it correctly. I have a three-layer project, initially did the same through repo and uow, later removed the repositories and uow, but when I add objects from different classes to each other I catch: System.InvalidOperationException: "It was not possible to determine the connection between the two objects, as they are tied to different ObjectContext objects. " As I understand it, this is due to the fact that every time I declare a new instance of the data context, this is what I have right now in the BLL:
public class EntityService : IEntityService { private MyContext db; public EntityService(string connectionString) { db = new MyContext(connectionString); } ... } How can I correctly transfer one instance of the data context to these services without explicitly re-implementing UoW?
Detachobject from one context, thenAttachto another context? - Alexander Petrov