无法创建类型为“”的“DbContext”。异常“实体类型‘Money’需要定义主键

问题描述 投票:0回答:1

我有 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

数据库显示某些属性应该是“Money”类型 enter image description here

entity-framework
1个回答
-1
投票

Money
必须配置为 EntityComplex Type

如果您尝试映射到数据库类型“Money”,则该属性应为 Decimal 类型。

© www.soinside.com 2019 - 2024. All rights reserved.