属性 'Dish_ID' 不能用作实体上的键 属性
The property 'Dish_ID' cannot be used as a key property on the entity
我正在尝试使用 EntityFramework 添加一些实体。
我需要与图片中相同的模型
我创建了两个 类:
public class PriceOfDish
{
[Key]
public virtual List<Dish> Dish_ID { get; set; }
[Key]
public DateTime DateTime { get; set; }
public decimal Price { get; set; }
}
public class Dish
{
[Key]
public int Dish_ID { get; set; }
public string DishName { get; set; }
public string Description { get; set; }
public virtual FoodCategory FoodCategory_ID { get; set; }
public virtual Feature Feature_ID { get; set; }
public virtual ICollection<OrderedDishes> Orders { get; set; }
}
使用 FluentAPI 尝试设置主键:
builder.Entity<PriceOfDish>()
.HasKey(t => new {t.Dish_ID, t.DateTime});
在更新数据库期间我收到错误消息:"The property 'Dish_ID' cannot be used as a key property on the entity 'testFOef.PriceOfDish' because the property type is not a valid key type. Only scalar types, string and byte[] are supported key types."。
但为什么?你能给我解释一下吗?感谢您的帮助
就像错误说的那样,您正在使用 List<Dish>
作为主键的类型。您必须像往常一样使用标量类型(值类型)或 byte[].
对您来说,解决方案是创建 Dish_ID
类型 int
的属性。
public class PriceOfDish
{
[Key]
public int Dish_ID { get; set; }
[Key]
public DateTime DateTime { get; set; }
[ForeignKey("Dish_Id")]
public virtual Dish Dish { get; set; }
public decimal Price { get; set; }
}
您正在尝试将 List<Dish> Dish_ID
设置为 table 的键。
但这不受支持。这也没有多大意义。 Dish
的列表将有一个 Price
?我想你想把 int Dish_ID
放在那里
我正在尝试使用 EntityFramework 添加一些实体。
我需要与图片中相同的模型
public class PriceOfDish
{
[Key]
public virtual List<Dish> Dish_ID { get; set; }
[Key]
public DateTime DateTime { get; set; }
public decimal Price { get; set; }
}
public class Dish
{
[Key]
public int Dish_ID { get; set; }
public string DishName { get; set; }
public string Description { get; set; }
public virtual FoodCategory FoodCategory_ID { get; set; }
public virtual Feature Feature_ID { get; set; }
public virtual ICollection<OrderedDishes> Orders { get; set; }
}
使用 FluentAPI 尝试设置主键:
builder.Entity<PriceOfDish>()
.HasKey(t => new {t.Dish_ID, t.DateTime});
在更新数据库期间我收到错误消息:"The property 'Dish_ID' cannot be used as a key property on the entity 'testFOef.PriceOfDish' because the property type is not a valid key type. Only scalar types, string and byte[] are supported key types."。 但为什么?你能给我解释一下吗?感谢您的帮助
就像错误说的那样,您正在使用 List<Dish>
作为主键的类型。您必须像往常一样使用标量类型(值类型)或 byte[].
对您来说,解决方案是创建 Dish_ID
类型 int
的属性。
public class PriceOfDish
{
[Key]
public int Dish_ID { get; set; }
[Key]
public DateTime DateTime { get; set; }
[ForeignKey("Dish_Id")]
public virtual Dish Dish { get; set; }
public decimal Price { get; set; }
}
您正在尝试将 List<Dish> Dish_ID
设置为 table 的键。
但这不受支持。这也没有多大意义。 Dish
的列表将有一个 Price
?我想你想把 int Dish_ID
放在那里