ef code first - Delete records from relationship tables in entity framework -
i have created following entity classess db tables
public class jewelry { #region properties public jewelry_items jewelryitems { get; set; } public jewelry_addnl_details jewelryaddnldetails { get; set; } #endregion } public class jewelry_items { #region properties [key] public int jewelryid { get; set; } public int generalinfoid { get; set; } public string style { get; set; } public string jewelrytype { get; set; } public string jewelrydesc { get; set; } public string addnldetails { get; set; } public string otherdetails { get; set; } public string ringcut { get; set; } public string modelnumber { get; set; } public string createdby { get; set; } public datetime createddate { get; set; } public string lastmodifiedby { get; set; } public datetime lastmodifieddate { get; set; } #endregion } public class jewelry_addnl_details { #region properties [key] public int id { get; set; } public int jewelryid { get; set; } public string iscompleteddesc { get; set; } public string appraisaldate { get; set; } public int replacementvalue { get; set; } public string useonbehalf { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string relationship { get; set; } #endregion } jewelryid has fk relationship between 2 tables. want delete record 2 tables did following . there better way of doing this?
using (var db = new context()) { var jewelryitem = db.jewelryitemslist.singleordefault(x => x.jewelryid == jewelryid); var jewelryaddnldetail = db.jewelryaddnldetailslist.singleordefault (x => x.jewelryid == jewelryid); if (jewelryaddnldetail != null) { db.jewelryaddnldetailslist.remove(jewelryaddnldetail); } if (jewelryitem != null) { db.jewelryitemslist.remove(jewelryitem); } db.savechanges(); issuccess = true; } update :
new code
public class rli_jewelry_items { #region properties [key] public int jewelryid { get; set; } [foreignkey("generalinfoid")] public virtual rli_insured_generalinfo rli_insured_generalinfo { get; set; } public int generalinfoid { get; set; } public string style { get; set; } public string jewelrytype { get; set; } public string jewelrydesc { get; set; } public string addnldetails { get; set; } public string otherdetails { get; set; } public string ringcut { get; set; } public string modelnumber { get; set; } public string createdby { get; set; } public datetime createddate { get; set; } public string lastmodifiedby { get; set; } public datetime lastmodifieddate { get; set; } #endregion } public class rli_jewelry_addnl_details { #region properties [key] public int id { get; set; } [required] [foreignkey("jewelryid")] public virtual rli_jewelry_items rli_jewelry_items { get; set; } [required] public int jewelryid { get; set; } public string iscompleteddesc { get; set; } public string appraisaldate { get; set; } public int replacementvalue { get; set; } public string useonbehalf { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string relationship { get; set; } #endregion }
Comments
Post a Comment