asp.net mvc 3 - How to Delete via DbSet in EntityFramework -
hi i'm trying write generic repository delete operation , repository
public class repository<t> : irepository<t> t : class, iaggregateroot { private readonly dbset<t> _entityset; private readonly statoscontext _statoscontext; public repository(statoscontext statoscontext) { _statoscontext = statoscontext; _entityset = statoscontext.set<t>(); } public void add(t entity) { _entityset.add(entity); } public void delete(t entity) { _entityset.remove(entity); } }
when call delete via service method
public void removecontact(contactviewmodel contactviewmodel) { var categoryview = new contactviewmodel { contactid = contactviewmodel.contactid }; var contact = categoryview.converttocontactmodel(); _contactrepository.delete(contact); _contactrepository.savechanges(); }
it doesn't work because doesn't find entity how can write delete method in mt generic repository ??
the issue entity isnt attached yet.
heres generic repository, take how this
public void removeonsave(t entity) { try { var e = m_context.entry(entity); if (e.state == entitystate.detached) { m_context.set<t>().attach(entity); e = m_context.entry(entity); } e.state = entitystate.deleted; } catch (invalidoperationexception ex) { throw new repositorytrackingexception( "an attempt made delete entity modifying, may happen if trying update using same repository instance in 2 place", ex); } }
Comments
Post a Comment