asp.net mvc - Will .Remove on entity framework perform a Cascade delete by defualt -


something strange happen. have table named group have one-to-many relation table named usergroups.

now using entity framework .remove method. able delete group have users, although when try similar operation directly on database raise exception (that group have child records!!!) idea happening !!!.

the action method looks :-

[httppost, actionname("delete")]         public actionresult deleteconfirmed(int id)         {             try             {                 var v = grouprepository.find(id).name;                  grouprepository.delete(id);                 grouprepository.save();                 return json(new { issuccess = "true", id = id, description = v }, jsonrequestbehavior.allowget);               //  return redirecttoaction("index");             }             catch (nullreferenceexception)             {                 //modelstate.addmodelerror(string.empty, " record might have been deleted.please refresh page.");                 return json(new { issuccess = "false" }, jsonrequestbehavior.allowget);              } return redirecttoaction("delete", new { id = id});         } 

the repository methods are:-

public void delete(int id)         {             var group = context.groups.find(id);             context.groups.remove(group);         }  public group find(int id)         {              return context.groups.where(c => c.groupid == id).include(a => a.usergroups)                 .include(a2 => a2.securityroles).singleordefault();         }  

i afraid since on find method retrieving group , 2 navigation properties , means .delete remove thses navigation first remove group object !!!!

edit

i have defined 2 method; find , findall :-

public group find(int id)         {              return context.groups.find(id) ;         }         public group findall(int id)         {              return context.groups.where(c => c.groupid == id).include(a => a.usergroups)                 .include(a2 => a2.securityroles).singleordefault();         } 

so other suggestions ??

yes will.

depending on use, virtual or foreign id field:

public virtual category {get; set;} 

or

public guid categoryid {get;set:} 

will change set in database table field's property allow null, check table designer see. virtual set true , allow null foreign key field set false allow null. if combine them still set false. advise combine them because allow pull sub entity out explicitly calling database again. ensure disable cascade on delete need set foreign key field null using ? operator. see below:

so, if do:

public virtual category {get;set;} 

it set allow null on category_id field in database table.

if do:

public guid categoryid {get;set;} public virtual category category {get;set;} 

it create field categoryid , not set allow null

but if do:

public guid? categoryid {get;set;} public virtual category category {get;set;} 

it create field categoryid , set allow null

i use guid primary key field, still works int. if wondering how use guid (i feel better) this:

[key, databasegenerated(databasegeneratedoption.identity)] public guid id {get;set;} 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -