c# - EF 6: many-to-many self-reference: The DELETE statement conflicted with the REFERENCE constraint -


i have entity:

public class course {     public course(){         postrequisites = new list<course>();         prerequisites = new list<course>();     }     public long id { get; set; }     public byte[] rowversion { get; set; }     // other properties...     public ilist<course> postrequisites { get; set; }     public ilist<course> prerequisites { get; set; }  } 

which has many-to-many self-referencing relationship. , here configuration:

public class courseconfiguration : entitytypeconfiguration<course> {     public courseconfiguration() {         // configurations other properties...         hasmany(t => t.postrequisites)             .withmany(t => t.prerequisites)             .map(t => {                 t.totable("courserequisites");                 t.mapleftkey("postrequisiteid");                 t.maprightkey("prerequisiteid");             });     } } 

when try delete item method (see method handle):

public class deletecommandhandlerbase {      private readonly entitycontext _context;      public deletecommandhandlerbase(entitycontext context) {         _context = context;     }      protected virtual void handle<tentity>(long entityid) tentity : entitybase, new() {         var entity = _context.set<tentity>().find(entityid);         if (entity == null)             throw new exception(textresources.itemtodeletenotfound);         _context.delete(entity); // see comment #1         _context.savechanges();     } }  // comment #1 // _context.delete custom method on context is: public void delete<tentity>(tentity entity) tentity : class {     dbset<tentity> set = set<tentity>();     if (entry(entity).state == entitystate.detached)         set.attach(entity);     set.remove(entity); } 

, error:

an error occurred while updating entries. see inner exception details.

which inner exception is:

an error occurred while updating entries. see inner exception details.

which inner exception is:

the delete statement conflicted reference constraint "fk_dbo.courserequisites_dbo.courses_postrequisiteid". conflict occurred in database "mydb", table "dbo.courserequisites", column 'postrequisiteid'. statement has been terminated.

do know what's going on?

i having same error. found cascade delete in many-to-many self-reference table

unfortunatelly no solution. in essence it's database problem. first need delete relationship entries, before can delete entity itself.

but since both have used fluent api, not have access relationship table, @ least don't know how.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -