簡體   English   中英

如何使用Entity Framework僅使用一側的導航屬性創建多個多對多關系?

[英]How to create multiple many-to-many relationships with only one-sided navigation properties using Entity Framework?

我有兩個使用Entity Framework的代碼優先API映射到POCO實體的表。

這些表是CatalogValuePerson

我想通過Person: Addresses and Organisations的導航屬性在這些表之間創建2個多對多關系。

我不得不提到我不想將導航屬性添加到CatalogValue

這是我當前的代碼:

 modelBuilder.Entity<Person>()
             .HasMany(a => a.Addresses)
             .WithOptional()
             .Map(c =>
             {
                  c.ToTable("PersonAddressMapping");
             });

 modelBuilder.Entity<Person>()
             .HasMany(a => a.Organisations)
             .WithOptional()
             .Map(c =>
             {
                  c.ToTable("PersonOrganisationMapping");
             });

這是我得到的錯誤:

The specified table 'PersonAddressMapping' was not found in the model. 
Ensure that the table name has been correctly specified.

我認為您在這里對WithOptional()是不正確的。 要配置多對多,您可以在HasMany()之后使用WithMany() HasMany() 您仍然不必指定集合。 嘗試像這樣配置。

 modelBuilder.Entity<Person>()
         .HasMany(a => a.Addresses)
         .WithMany()
         .Map(c =>
         {
              c.ToTable("PersonAddressMapping");
         });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM