asp.net mvc - Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths -
i have following entites in mvc project (code first approach) want know what's reason getting following error
introducing foreign key constraint 'fk_dbo.vendordetails_dbo.states_stateid' on table 'vendordetails' may cause cycles or multiple cascade paths. specify on delete no action or on update no action, or modify other foreign key constraints. not create constraint. see previous errors.
i wanted know tables getting multiple cascade paths diagram effective , should write using fluent api this: modelbuilder.entity<...>() .hasrequired(...) .withmany(...) .hasforeignkey(...) .willcascadeondelete(false);
public class vendordetails { [key] [databasegenerated(databasegeneratedoption.identity)] public int vendorid { get; set; } [maxlength(60)] public string vendorname { get; set; } public int vendortypeid { get; set; } public int countryid { get; set; } public int stateid { get; set; } [notmapped] public string countryname { get; set; } [notmapped] public string statename { get; set; } [notmapped] public string vendortypename { get; set; } public virtual country country { get; set; } public virtual state state { get; set; } public virtual vendortype vendortype { get; set; } } public class vendortype { [key] [databasegenerated(databasegeneratedoption.identity)] public int vendortypeid { get; set; } public string vendortypename { get; set; } public virtual icollection<vendordetails> vendors { get; set; } } public class country { [key] [databasegenerated(databasegeneratedoption.identity)] public int countryid { get; set; } public string countryname { get; set; } public virtual icollection<state> states { get; set; } } public class state { [key] [databasegenerated(databasegeneratedoption.identity)] public int stateid { get; set; } public string statename { get; set; } public int countryid { get; set; } public virtual country country { get; set; } }
you're getting because country has link state, , vendordetails has link both country , state. gives multiple paths between vendordetails , state - 1 through country , 1 direct.
i disable cascade delete on link state vendordetails:
modelbuilder .entity<vendordetails>() .hasoptional(e => e.state) .withmany() .willcascadeondelete(false);
Comments
Post a Comment