Entity Framework 6 - 添加包含其他子项的子项的父项
Entity Framework 6 - Add parent with children which contains other children
我在向 DbSet 添加对象时遇到问题。
class Word
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID {get;set;}
public virtual ICollection<Translation> Translations {get;set;}
public virtual ICollection<Inflection> Inflections {get;set;}
}
class Inflection
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID {get;set;}
public virtual Word Word {get;set;}
}
class Translation
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID {get;set;}
public virtual Word Word {get;set;}
public virtual ICollection<Sentence> Sentences {get;set;}
}
class Sentence
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID {get;set;}
public virtual Translation Translation {get; set;}
}
我这样做:
DatabaseContext.UserWord.Add(entity);
DatabaseContext.SaveChanges();
这是我的关系配置:
modelBuilder.Entity<Translation>()
.HasRequired(x => x.Word)
.WithMany(x => x.Translations);
modelBuilder.Entity<Inflection>()
.HasRequired(x => x.Word)
.WithMany(x => x.Inflections);
modelBuilder.Entity<Sentence>()
.HasRequired(x => x.Translation)
.WithMany(x => x.Sentences);
我尝试创建 Word 对象:
{
"Id": "GUID"
"Translations": [
{
"Id": "GUID",
"Sentences": [
{
"Id": "GUID",
}
]
}
],
"Inflections": [
{
"Id": "GUID",
}
]
}
但是 "DatabaseContext.SaveChanges()" 抛出异常:
The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_dbo.Sentence_dbo.Word_WordId". The conflict occurred in database
"tempDatabase", table "dbo.Word", column 'Id'. The statement has
been terminated.
以下 Word 对象一切正常:
{
"Id": "GUID"
"Translations": [
{
"Id": "GUID"
}
],
"Inflections": [
{
"Id": "GUID",
}
]
}
我做错了什么?
你能帮帮我吗?
看起来你的 sentence
模型已经改变了,因为它有指向 word
的外键,而它应该引用 translation
,请尝试 运行 add-migration
检查是否看到更改和 update-database
(如果您使用的是代码优先迁移)
我在向 DbSet 添加对象时遇到问题。
class Word
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID {get;set;}
public virtual ICollection<Translation> Translations {get;set;}
public virtual ICollection<Inflection> Inflections {get;set;}
}
class Inflection
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID {get;set;}
public virtual Word Word {get;set;}
}
class Translation
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID {get;set;}
public virtual Word Word {get;set;}
public virtual ICollection<Sentence> Sentences {get;set;}
}
class Sentence
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID {get;set;}
public virtual Translation Translation {get; set;}
}
我这样做:
DatabaseContext.UserWord.Add(entity);
DatabaseContext.SaveChanges();
这是我的关系配置:
modelBuilder.Entity<Translation>()
.HasRequired(x => x.Word)
.WithMany(x => x.Translations);
modelBuilder.Entity<Inflection>()
.HasRequired(x => x.Word)
.WithMany(x => x.Inflections);
modelBuilder.Entity<Sentence>()
.HasRequired(x => x.Translation)
.WithMany(x => x.Sentences);
我尝试创建 Word 对象:
{
"Id": "GUID"
"Translations": [
{
"Id": "GUID",
"Sentences": [
{
"Id": "GUID",
}
]
}
],
"Inflections": [
{
"Id": "GUID",
}
]
}
但是 "DatabaseContext.SaveChanges()" 抛出异常:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Sentence_dbo.Word_WordId". The conflict occurred in database "tempDatabase", table "dbo.Word", column 'Id'. The statement has been terminated.
以下 Word 对象一切正常:
{
"Id": "GUID"
"Translations": [
{
"Id": "GUID"
}
],
"Inflections": [
{
"Id": "GUID",
}
]
}
我做错了什么? 你能帮帮我吗?
看起来你的 sentence
模型已经改变了,因为它有指向 word
的外键,而它应该引用 translation
,请尝试 运行 add-migration
检查是否看到更改和 update-database
(如果您使用的是代码优先迁移)