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