0
//Repository Method

public void Delete(int id)
{
    using (var scope = new UnitOfWork(_container))
    {
        var entity = AuthorService.GetById(id);
        scope.Container.Authors.DeleteObject(entity);
    }     
}

Ninject binding

public class LibraryManagerInjectModule : NinjectModule
{
    public override void Load()
    {
        Bind<LibManagerContainer>().To<LibManagerContainer>().InThreadScope();
    }
}

//Author Service Class

public static class AuthorService
{
    private static LibManagerContainer Container
    {
        get { return MF.MF.Get<LibManagerContainer>(); }
    }

    public static Author GetById(int id)
    {
        using (var scope = new UnitOfWork(Container))
        {
            return Container.Authors.SingleOrDefault(x => x.Id == id);
        }
    }

    public static Author GetByName(String name)
    {
        using (var scope = new UnitOfWork(Container))
        {
            return Container.Authors.SingleOrDefault(x => x.Name == name);
        }
    }
}

Using this code i m not able to delete the entity from database. it show me an error that entity not belong to same object state manager but i create the libContainer object inThreadscope but never able to delete the entity.

Konamiman
  • 49,681
  • 17
  • 108
  • 138
Ankit Rana
  • 383
  • 6
  • 24

2 Answers2

2

You don't need to load entity from db to delete record. Id is enough to know, and it resolves another context problem:

var employer = new Employ { Id = 1 };
ctx.Employ.Attach(employer);
ctx.Employ.Remove(employer);
ctx.SaveChanges();

Taken from here: Delete a single record from Entity Framework?

P.S. Something wrong with your architecture. A single context should be used within one scope.

Community
  • 1
  • 1
Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114
  • I think this is not standardized way for deleting the object,but Thanks a lot for your reply – Ankit Rana Oct 25 '15 at 17:30
  • @AnkitRana, this approach produces one SQL command instead of two, and, moreover, it is the official tip from MSDN: http://blogs.msdn.com/b/alexj/archive/2009/03/27/tip-9-deleting-an-object-without-retrieving-it.aspx – Ilya Chumakov Oct 25 '15 at 20:43
1

The DataContext you use to retrieve the entity tracks the entity, to be aware of changes to it. Because of that, you are not able to save entities retrieved from one DataContext (or UnitOfWork in your case) using another.
As you mentioned in the comments, deleting should be another transaction. To achieve this you should delete by id, not the object.
Just add a RemoveById method to AuthorService:

public  static class AuthorService
{
    ...

    public static void RemoveById(int id)
    {
        using (var scope = new UnitOfWork(Container))
        {
            var author = Container.Authors.SingleOrDefault(x => x.Id == id);
            Container.Authors.Remove(author);
        }
    }
    ...
Domysee
  • 12,718
  • 10
  • 53
  • 84
  • but these two trans, action is different different transaction, one is read entity from database and another is delete the selected entity. I m using the Ninject DbContext in Threadscope. then why we need to pass scope. – Ankit Rana Oct 25 '15 at 07:44
  • because you are deleting by `object`. If you want these to be 2 different transactions, you'd have to delete by id. – Domysee Oct 25 '15 at 08:01
  • Thank for your reply, my concerned is to shared the EF Container instance to all our web app.but i think each container object has its own state management for object. – Ankit Rana Oct 25 '15 at 17:28