foreign keys - Entity Framework DELETE statement conflicted with the REFERENCE constraint -
i’m pretty new ef , have little problem.
i want delete item in database. i’m using sql server 2012 express, vs2012, adventureworks 2012.
the query execute following:
context = new awentities(); var removeditem = context.addresses .include("stateprovince") .include("salesorderheaders") .include("businessentityaddresses").single(d => d.addressid == 11); context.addresses.remove(removeditem); context.savechanges();
the error is
the delete statement conflicted reference constraint "fk_salesorderheader_address_shiptoaddressid". conflict occurred in database "adventureworks2012", table "sales.salesorderheader", column 'shiptoaddressid'. statement has been terminated.
is way delete items , according entries in other tables?
please point me right direction.
public partial class address { public address() { this.businessentityaddresses = new hashset<businessentityaddress>(); this.salesorderheaders = new hashset<salesorderheader>(); } public int addressid { get; set; } public string addressline1 { get; set; } public string addressline2 { get; set; } public string city { get; set; } public int stateprovinceid { get; set; } public string postalcode { get; set; } public system.data.spatial.dbgeography spatiallocation { get; set; } public system.guid rowguid { get; set; } public system.datetime modifieddate { get; set; } public virtual stateprovince stateprovince { get; set; } public virtual icollection<businessentityaddress> businessentityaddresses { get; set; } public virtual icollection<salesorderheader> salesorderheaders { get; set; } } public partial class stateprovince { public stateprovince() { this.addresses = new hashset<address>(); this.salestaxrates = new hashset<salestaxrate>(); } public int stateprovinceid { get; set; } public string stateprovincecode { get; set; } public string countryregioncode { get; set; } public bool isonlystateprovinceflag { get; set; } public string name { get; set; } public int territoryid { get; set; } public system.guid rowguid { get; set; } public system.datetime modifieddate { get; set; } public virtual icollection<address> addresses { get; set; } public virtual countryregion countryregion { get; set; } public virtual icollection<salestaxrate> salestaxrates { get; set; } public virtual salesterritory salesterritory { get; set; } } } public partial class businessentityaddress { public int businessentityid { get; set; } public int addressid { get; set; } public int addresstypeid { get; set; } public system.guid rowguid { get; set; } public system.datetime modifieddate { get; set; } public virtual address address { get; set; } public virtual addresstype addresstype { get; set; } public virtual businessentity businessentity { get; set; } } public partial class salesorderheader { public salesorderheader() { this.salesorderdetails = new hashset<salesorderdetail>(); this.salesorderheadersalesreasons = new hashset<salesorderheadersalesreason>(); } public int salesorderid { get; set; } public byte revisionnumber { get; set; } public system.datetime orderdate { get; set; } public system.datetime duedate { get; set; } public nullable<system.datetime> shipdate { get; set; } public byte status { get; set; } public bool onlineorderflag { get; set; } public string salesordernumber { get; set; } public string purchaseordernumber { get; set; } public string accountnumber { get; set; } public int customerid { get; set; } public nullable<int> salespersonid { get; set; } public nullable<int> territoryid { get; set; } public int billtoaddressid { get; set; } public int shiptoaddressid { get; set; } public int shipmethodid { get; set; } public nullable<int> creditcardid { get; set; } public string creditcardapprovalcode { get; set; } public nullable<int> currencyrateid { get; set; } public decimal subtotal { get; set; } public decimal taxamt { get; set; } public decimal freight { get; set; } public decimal totaldue { get; set; } public string comment { get; set; } public system.guid rowguid { get; set; } public system.datetime modifieddate { get; set; } public virtual address address { get; set; } public virtual shipmethod shipmethod { get; set; } public virtual creditcard creditcard { get; set; } public virtual currencyrate currencyrate { get; set; } public virtual customer customer { get; set; } public virtual icollection<salesorderdetail> salesorderdetails { get; set; } public virtual salesperson salesperson { get; set; } public virtual salesterritory salesterritory { get; set; } public virtual icollection<salesorderheadersalesreason> salesorderheadersalesreasons { get; set; } }
can't tell have said, may benefit looking using dbmodelbuilder solve cascade issues:
modelbuilder.entity<parent>() .hasmany<child>(c => c.children) .withoptional(x => x.parent) .willcascadeondelete(true);
again, need more information model structure determine if right approach.
either or in delete method, remove children first, , remove parent.
Comments
Post a Comment