【发布时间】:2020-06-07 23:34:18
【问题描述】:
我遇到了类似的错误:The property X is of type Y which is not supported by current database provider
但有一个稍微不同的问题:
我有以下课程:
public class Apple
{
public Box Box{ get; set; }
public int R { get => Color.R; set => Color = new Color (value, Color.G, Color.B); }
public int G { get => Color.G set => Color = new Color (Color.R, value, Color.B); }
public int B { get => Color.B set => Color = new Color (Color.R, Color.G, value); }
[NotMapped]
public Color Color { get; set; }
}
public class Box
{
[Key]
public int ID { get; set; }
public IEnumerable<Apple> Apples { get; set; }
}
在 DbContext 类中:
modelBuilder.Entity<Apple>()
.HasKey(a=> new { a.Box, a.R, a.G, a.B });
但它似乎不起作用并给我错误:“属性'Apple.Box'是当前数据库提供程序不支持的'Box'类型。更改属性CLR类型或使用忽略属性'[NotMapped]' 属性或在 'OnModelCreating' 中使用 'EntityTypeBuilder.Ignore'
【问题讨论】:
-
您为什么要将复杂类型(即
Box)注释为复合键的一部分? -
@CodeCaster 所以我猜基本答案是密钥必须是数据库类型而不是 CLR 类型。不幸的是,错误消息没有提及任何关于键的内容,这使得它令人困惑,因为只要它们不是键,就可以将其他 CLR 类型作为属性。
标签: entity-framework entity-framework-core ef-core-3.1