2015-10-17 63 views
0

我幾乎是一個菜鳥,所以我會很感激我的代碼的一些評論,因爲我在這裏自己學習。如果我正確地做了他們,我想對我的關係映射提供一些反饋,因爲我現在有疑問。 (任何提示如何更正確地檢查我自己也是受歡迎的!)製作多個表並添加1:1..0的關係

我有一個數據庫與用戶,但我的一些用戶是供應商。當然這是一個選項。起初我只是認爲我會分配一個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); 

    } 
} 

和i n我的會員資格映射:

 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?

回答

0

一些映射是基於觀點的,這意味着有些時候沒有「正確的答案」。如果我理解正確的情況下,我建議你做這些改變:

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

關於SupplierReview,它不需要MembershipUser財產,只是Supplier屬性:

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

希望它能幫助!

+0

嘿,首先感謝我的代碼審查,我非常感謝! 我之所以將MembershipUser添加到供應商審覈中,是因爲供應商從標準用戶處獲得審覈,以便我可以收集使用我的網站的供應商的質量/交付時間/價格/回覆的數據。所以審查是關於供應商,由用戶提出。 – Curator

+0

對不起。是的,這是正確的! –