制作多个表并添加1:1..0的关系

时间:2015-10-17 14:36:51

标签: c# entity-framework

我几乎是一个菜鸟,所以我很感激我的代码评论,因为我在这里学习。如果我正确地做了它,我想对我的关系映射做一些反馈,因为我现在有疑问。 (关于如何更正确地检查这个问题的任何提示也欢迎!)

我有一个用户数据库,但我的一些用户是供应商。当然,这是一种选择。起初我以为我会给它分配一个bool来存储它在我的数据库中,但现在我正在扩展它以便我的供应商可以得到评论。

这就是我的方式。 我的供应商型号:

    public partial class Suppliers
    {
    public Suppliers()
    {
        Id = GuidComb.GenerateComb();
    }
    public Guid Id { get; set; }
    public virtual IList<SupplierReview> SupplierReviews { get; set; }
    public virtual MembershipUser user { get; set; }
    }

然后是我的评论模型:

public partial class SupplierReview : Entity
{
    //int: 0-255; score can only be 10 so this is a good fit.
    private int answerScore;
    private int timeScore;
    private int priceScore;
    private int qualityScore;

    public SupplierReview()
    {
        Id = GuidComb.GenerateComb();
        qualityScore = 5;
        timeScore = 5;
        priceScore = 5;
        answerScore = 5;
    }
    public Guid Id { get; set; }

    //these sets make sure that the score can only be one to 10
    public int QualityScore
    {
        get
        {
            return qualityScore;
        }
        set
        {
            qualityScore = value % 10;
        }
    }
    public int TimeScore
    {
        get
        {
            return timeScore;
        }
        set
        {
            timeScore = value % 10;
        }
    }
    public int PriceScore
    {
        get
        {
            return priceScore;
        }
        set
        {
            priceScore = value % 10;
        }
    }
    public int AnswerScore
    {
        get
        {
            return answerScore;
        }
        set
        {
            answerScore = value % 10;
        }
    }
    public string Comment { get; set; }

    public virtual MembershipUser User { get; set; }
    public virtual Suppliers Supplier { get; set; }
}

然后到我添加的成员模型:

    public virtual IList<SupplierReview> SupplierReviews { get; set; }
    public virtual Suppliers IsSupplier { get; set; }

然后在我的映射中:

public class SuppliersMapping : EntityTypeConfiguration<Suppliers>
{
    public SuppliersMapping()
    {
        HasKey(x => x.Id);
        Property(x => x.Id).IsRequired();

        HasMany(x => x.SupplierReviews).WithRequired(x => x.Supplier)
            .Map(x => x.MapKey("Suppliers_Id"))
            .WillCascadeOnDelete(false);
    }
}

public class SupplierReviewMapping : EntityTypeConfiguration<SupplierReview>
{
    public SupplierReviewMapping()
    {
        HasKey(x => x.Id);
        Property(x => x.Id).IsRequired();

        Property(x => x.Comment).IsOptional().HasMaxLength(1000);
        Property(x => x.QualityScore).IsOptional();
        Property(x => x.PriceScore).IsOptional();
        Property(x => x.TimeScore).IsOptional();
        Property(x => x.AnswerScore).IsOptional();

        HasRequired(x => x.User).WithMany(x => x.SupplierReviews)
            .Map(x => x.MapKey("MembershipUser_Id"))
            .WillCascadeOnDelete(false);

        HasRequired(t => t.Supplier).WithMany(t =>t.SupplierReviews)
            .Map(m => m.MapKey("Suppliers_Id"))
            .WillCascadeOnDelete(false);

    }
}

并在我的会员映射中:

        HasOptional(x => x.IsSupplier)
            .WithOptionalDependent()
            .Map(p => p.MapKey("Suppliers_Id"));
        HasMany(x=>x.SupplierReviews).WithRequired(x => x.User)
            .Map(x=> x.MapKey("MembershipUser_Id"))
            .WillCascadeOnDelete(false);

感谢您阅读这篇文章。所以,我的疑问是一般的映射,如果我正确使用WillCascadeOnDelete?

1 个答案:

答案 0 :(得分:0)

有些映射是基于意见的,这意味着有时候没有&#34;正确答案&#34;。如果我理解您的情况,我建议您进行这些更改:

  1. 如果User可以是Supplier,但并非所有供应商都是User

    public class MembershipUser
    {
        public Guid MembershipUserId { get; set; }
    
        public IsSupplier { get; set; } //I like to have a discriminator field
    
        //if it is a supplier, there is a relationship with the supplier table
        //it it is not, the property will be null
        public Supplier Supplier { get; set; } 
    }
    
    public class Supplier
    {
         //pk 
         public Guid SupplierId { get; set; }             
    
         //navigation property
         public MembershipUser User { get; set; }
    
         //... other properties
    }
    

    映射:

     modelBuilder.Entity<MembershipUser>()
         .HasKey(i => i.MembershipUserId);
    
     modelBuilder.Entity<MembershipUser>()
        .HasOptional(i => i.Supplier)
        .WithOptionalPrincipal(i => i.User)
        .Map(i => i.MapKey("MemberhsipUserId"))
        .WillCascadeOnDelete(false);
    
     modelBuilder.Entity<Supplier>()
        .HasKey(i => i.SupplierId);
    
  2. 如果所有供应商都是User

    public class Supplier
    {
        //The SupplierPK must be the MembershipUser Fk
        public Guid MembershipUserId { get; set; }             
    
        //navigation property, in case of the supplier is an user
        public MembershipUser User { get; set; }
    
        //... other properties
    }
    

    映射:

    modelBuilder.Entity<MembershipUser>()
       .HasOptional(i => i.Supplier)
       .WithRequired(i => i.User)
       .WillCascadeOnDelete(false);
    
  3. 关于SupplierReview,它不需要MembershipUser属性,只需要Supplier属性:

    modelBuilder.Entity<SupplierReview>()
      .HasRequired(i => i.Supplier)
      .WithMany(i => i.SupplierReviews)
      .HasForeignKey(i => i.SupplierId)
      .WillCascadeOnDelete(false);
    

    希望它有所帮助!