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
对不起。是的,这是正确的! –