0

I have created following entity classess for my 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 a FK relationship between two tables. I want to delete the record from two tables so I did following . Is there any 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
    }
nimish jain
  • 141
  • 1
  • 13
  • 1
    If you always want to delete the loaded child, you can configure cascaded deletes to be on for the relationship and then just delete the parent. http://stackoverflow.com/questions/5448702/cascading-deletes-with-entity-framework-related-entities-deleted-by-ef – Steve Greene Jul 07 '15 at 18:39
  • I tried it to use Cascade delete using data anotation but still getting fk contraint error when I delete the item from JewelryItemsList – nimish jain Jul 07 '15 at 21:07
  • It must also be cascaded delete in the database. – Gert Arnold Jul 07 '15 at 21:33
  • If I set up in database then why would have to set up in my code first entities. – nimish jain Jul 07 '15 at 21:39
  • Read the answer in the link."Cascade deletion when using EF needs two steps..." – Steve Greene Jul 08 '15 at 12:42

0 Answers0