C# EF Core:无法跟踪实体类型的实例,因为另一个实例具有相同的键值
假设我们有以下两个数据库表:C# EF Core:无法跟踪实体类型的实例,因为另一个实例具有相同的键值,c#,.net,entity-framework,asp.net-core,entity-framework-core,C#,.net,Entity Framework,Asp.net Core,Entity Framework Core,假设我们有以下两个数据库表: Foo: > FooId (PK) > FooName Bar: > BarId (PK, FK) > Comment > Other columns... 我有以下EF映射: [Table("Foo")] public class Foo { [Key] public long FooId { get; set; } public string FooName { get; set; } //
Foo:
> FooId (PK)
> FooName
Bar:
> BarId (PK, FK)
> Comment
> Other columns...
我有以下EF映射:
[Table("Foo")]
public class Foo
{
[Key]
public long FooId { get; set; }
public string FooName { get; set; }
// (0-n) relation
// public List<Bar> Bars { get; set; }
}
[Table("Bar")]
public class Bar
{
// PK/FK
[Key, ForeignKey("Foo")]
public long BarId { get; set; }
public string Comment { get; set; }
}
EF认为“BarId”必须是唯一的,因为属性被标记为“Key”,但它是一对多关系中的PK/FK(“Foo”可以有0个或多个“Bar”),我在这里遗漏了什么?如果
Foo
可以有零个或多个Bar
s,这是一对多关系。如果关系为一对零或一,则通常会将键创建为PrimaryKey
和ForiegnKey
。因此,根据您的要求,您的模型应更像以下:
[Table("Foo")]
public class Foo
{
[Key]
public long FooId { get; set; }
public string FooName { get; set; }
public virtual List<Bar> Bars { get; set; }
}
[Table("Bar")]
public class Bar
{
[Key]
public long BarId { get; set; }
public long FooId { get; set; }
[ForeignKey("FooId")]
public virtual Foo Foo { get; set; }
public string Comment { get; set; }
}
[表(“Foo”)]
公开课Foo
{
[关键]
公共长FooId{get;set;}
公共字符串FooName{get;set;}
公共虚拟列表栏{get;set;}
}
[表格(“酒吧”)]
公共类酒吧
{
[关键]
公共长BarId{get;set;}
公共长FooId{get;set;}
[外键(“FooId”)]
公共虚拟Foo Foo{get;set;}
公共字符串注释{get;set;}
}
如果Foo
可以有零个或多个条
s,则是一对多关系。如果关系为一对零或一,则通常会将键创建为PrimaryKey
和ForiegnKey
。因此,根据您的要求,您的模型应更像以下:
[Table("Foo")]
public class Foo
{
[Key]
public long FooId { get; set; }
public string FooName { get; set; }
public virtual List<Bar> Bars { get; set; }
}
[Table("Bar")]
public class Bar
{
[Key]
public long BarId { get; set; }
public long FooId { get; set; }
[ForeignKey("FooId")]
public virtual Foo Foo { get; set; }
public string Comment { get; set; }
}
[表(“Foo”)]
公开课Foo
{
[关键]
公共长FooId{get;set;}
公共字符串FooName{get;set;}
公共虚拟列表栏{get;set;}
}
[表格(“酒吧”)]
公共类酒吧
{
[关键]
公共长BarId{get;set;}
公共长FooId{get;set;}
[外键(“FooId”)]
公共虚拟Foo Foo{get;set;}
公共字符串注释{get;set;}
}
这是否回答了您的问题?如果你试图用你的Foo和Bar类型创建一个简短的、独立的repo,你可能会自己找到答案。否则,将其作为问题的一部分发布。这是否回答了您的问题?如果你试图用你的Foo和Bar类型创建一个简短的、独立的repo,你可能会自己找到答案。否则,将其作为问题的一部分发布。“BarId”在“Bar”表中不相关,“FooId”已经是同一表中的PK/FK。获取链接到“FooId”的“Bar”列表的典型SQL查询如下:从其中FooId=1的Bar中选择*。“BarId”不会在任何地方使用。@Karim为什么要将FooId同时用作PK和FK?只有当一个Foo可以有0或1个Bar时,您才会这样做。但是你的要求是一个Foo可能有0个或多个barsye你是对的,我确实需要一个BarId如果我想有一个零对多关系,“BarId”在“Bar”表中不相关,“FooId”已经是同一表中的PK/FK。获取链接到“FooId”的“Bar”列表的典型SQL查询如下:从其中FooId=1的Bar中选择*。“BarId”不会在任何地方使用。@Karim为什么要将FooId同时用作PK和FK?只有当一个Foo可以有0或1个Bar时,您才会这样做。但你的要求是一个Foo可能有0个或多个barsye你是对的,如果我想有一个零对多的关系,我确实需要一个BarId
[Table("Foo")]
public class Foo
{
[Key]
public long FooId { get; set; }
public string FooName { get; set; }
public virtual List<Bar> Bars { get; set; }
}
[Table("Bar")]
public class Bar
{
[Key]
public long BarId { get; set; }
public long FooId { get; set; }
[ForeignKey("FooId")]
public virtual Foo Foo { get; set; }
public string Comment { get; set; }
}