简体   繁体   English

使用 Fluent API 使用 Entity Framework 6 在 asp.net 中配置一对多关系

[英]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.

相关问题 ASP.NET 3.1 Web API,实体框架一对多关系要么没有建立要么以循环结束 - ASP.NET 3.1 Web API, Entity Framework one to many relationship either not made or ending in cycle 使用Fluent API在实体框架中创建一对多关系 - Creating one to many relationship in Entity Framework using Fluent API 在实体框架6流畅的api中的视图上映射多对一关系 - Mapping a many to one relationship over a view in entity framework 6 fluent api 与Entity Framework Fluent API的一对一关系 - One to one relationship with Entity Framework Fluent API ASP.net MVC实体框架更新多对多关系 - ASP.net MVC Entity framework update Many to Many relationship 实体FrameWork使用Asp.Net的多对多关系 - Entity FrameWork Many to Many relationship using Asp.Net ASP.NET MVC 5和实体框架多对多关系 - ASP.NET MVC 5 & Entity Framework many to many relationship ASP.NET MVC3和实体框架 - 一个视图中的一对多关系 - ASP.NET MVC3 and Entity Framework- one-to-many relationship in one view 使用Entity Framework Code First和Fluent API配置许多一对一关系 - Configuring many One-To-One relationships with Entity Framework Code First and Fluent API ASP.Net实体框架一对多关系:如何建立依赖关系和延迟加载不起作用 - ASP.Net Entity Framework one-to-many relationship: How to establish dependency and lazy loading not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM