我有一个产品类:实体框架一到一个有一个是可选的关系抛出错误
public class Product
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
public decimal Price { get; set; }
public Offer Offer { get; set; }
}
和报价类:
public class Offer
{
public int ID { get; set; }
[Required]
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public decimal OfferPrice { get; set; }
public Product Product { get; set; }
}
基本上想我要实现的是,当报价中添加了与此优惠相关的产品。因此,优惠总是有产品,但产品并不总是需要优惠。如果产品确实有一个我希望能够通过Product.Offer
访问它,但在此设置,我得到更新数据库时出现以下错误信息:
Unable to determine the principal end of an association between the types 'WebshopISEC.Models.Offer' and 'WebshopISEC.Models.Product'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
我已经尝试了很多东西(数据注释和Fluent API)并且无休止地搜索,但我似乎无法解决此问题。我尝试过的流利的API方法:
modelBuilder.Entity<Offer>()
.HasRequired(x => x.Product)
.WithOptional(x => x.Offer);
但是,这工作无济于事,我该如何分配一个Principal End?和什么课程?
感谢您的回复!这会引发以下错误:'''“错误:新名称'ID'已经被用作COLUMN名称,并且会导致不允许的重复。'''' – Mirage
你可以尝试删除你的数据库然后更新数据库? – WannaCSharp
我已经通过服务器资源管理器手动删除所有表,现在我似乎无法让我的表回来..我如何重置迁移历史,并让它创建一个基于我当前类的全新数据库? – Mirage