我有 2 个型号类别,用于了解其他型号的价格:
public class ChefFoodPrice
{
[Key]
public int ChefFoodPriceId { get; set; }
public int ChefFoodId { get; set; }
public int PriceTypeId { get; set; }
public Money ChefFoodsPrice { get; set; }
public Money ChefFoodPriceIsDefault { get; set; }
public DateTime ChefFoodPriceFromDate { get; set; }
public DateTime ChefFoodPriceToDate { get; set; }
public DateTime RegisterDate { get; set; }
public int CreatedUser { get; set; }
#region Relations
[ForeignKey("PriceTypeId")]
public PriceType PriceType { get; set; }
#endregion
}
另一个模型类是:
public class PriceType
{
[Key]
public int PriceTypeId { get; set; }
public string PriceTypeName { get; set; }
public string PriceTypeCurrency{ get; set; }
public int PriceTypeIsDefault { get; set; }
public DateTime RegisterDate { get; set; }
public int CreatedUser { get; set; }
#region Relations
public ICollection<ChefFoodPrice> ChefFoodPrices { get; set; }
#endregion
}
但是添加迁移后我看到这个错误:
无法创建类型为“”的“DbContext”。异常“实体类型‘Money’需要定义主键”。如果您打算使用无键实体类型,请在“OnModelCreating”中调用“HasNoKey”。有关无密钥实体类型的更多信息,请参阅 https://go.microsoft.com/fwlink/?linkid=2141943。' 在尝试创建实例时抛出。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728
我添加了这段代码:
modelBuilder.Entity<ChefFoodPrice>().HasNoKey();
但我仍然收到该错误并且无法添加迁移。
我已经设置了金钱密钥?
添加
.HasNoKey()
后,我收到此错误:
无法创建类型为“”的“DbContext”。异常“实体类型‘Money’需要定义主键”。如果您打算使用无键实体类型,请在“OnModelCreating”中调用“HasNoKey”。有关无密钥实体类型的更多信息,请参阅 https://go.microsoft.com/fwlink/?linkid=2141943。' 在尝试创建实例时抛出。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728