从实体框架中的关系表中删除记录

时间:2015-07-07 16:22:22

标签: ef-code-first entity-framework-6

我为我的数据库表创建了以下实体classess

 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在两张桌子之间有一个FK关系。我想删除两个表中的记录,所以我做了以下。有没有更好的方法呢?

 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;
            }

更新:

新代码

 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
    }

0 个答案:

没有答案