[英]Configuring one to Many relationship in asp.net with Entity Framework 6 with Fluent API
I'm getting error before migration that says我在迁移之前收到错误,说
The navigation property quotes in Book is configured with conflicting municipalities Book 中的导航属性引用配置了冲突的自治市
Please help!请帮忙!
public partial class Book
{
public virtual ICollection<Quote> Quotes { get; set; }
}
public partial class Quote
{
public virtual Book Book { get; set; }
}
//Fluent Api
modelBuilder.Entity<Quote>()
.HasOptional(b => b.Book);
modelBuilder.Entity<Book>()
.HasMany<Quote>(s => s.Quotes)
.WithRequired(s => s.Book)
.HasForeignKey<int>(s => s.QuoteBookID);
You have written你写
modelBuilder.Entity<Quote>()
.HasOptional(b => b.Book);
modelBuilder.Entity<Book>()
.HasMany<Quote>(s => s.Quotes)
.WithRequired(s => s.Book)
.HasForeignKey<int>(s => s.QuoteBookID);
Which describes a self-contradictory state of affairs.它描述了一种自相矛盾的事态。 Quote.Book
cannot be both required and optional so we must make up our mind. Quote.Book
不能既是必需的又是可选的,所以我们必须下定决心。
If it is required then如果需要那么
modelBuilder.Entity<Book>()
.HasMany(s => s.Quotes)
.WithRequired(s => s.Book)
.HasForeignKey(s => s.QuoteBookID);
is all that is necessary.这就是必要的。 We need not configure the optionality我们不需要配置可选性
Id it is optional then id 它是可选的
modelBuilder.Entity<Book>()
.HasMany(s => s.Quotes)
.WithOptional(s => s.Book)
.HasForeignKey(s => s.QuoteBookID);
Note that I've removed the explicit type arguments as they are unnecessary.请注意,我删除了显式类型参数,因为它们是不必要的。
We can go a bit further in cleaning up and just write我们可以更进一步清理,然后写
modelBuilder.Entity<Book>()
.HasMany(s => s.Quotes)
.WithOptional() // or .WithRequired()
.HasForeignKey(s => s.QuoteBookID);
As there's no need to be overly specific因为没有必要过于具体
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.