[英]EF Fluent API Many To Many with different ID Field Names
在這個問題: Ef Many To Many ,一個關於如何手動指定鏈接表的答案。 但我有一個稍微獨特的情況(我肯定不是真的很獨特)。
我的兩個表都有一個Id字段。 EG: [dbo].[Account].[Id]
和[dbo].[Person].[Id]
。 我的Code-First中的每個表都有以下OnModelCreating:
modelBuilder.Entity<Account>.HasKey(x => x.Id);
modelBuilder.Entity<Person>.HasKey(x => x.Id);
但我的[dbo].[AccountsToPersons]...
表格有字段EG: [AccountId]
和[PersonId]
AccountsToPersons表不由代碼中的類表示。
我顯然已經有了一個現有的模型,但是我們使用EF Code-First Fluent API而不是從數據庫更新模型。
那么如何更改此代碼以使其與映射不同的ID列名稱一起使用?
public DbSet<Person> Persons { get; set; }
public DbSet<Account> Accounts { get; set; }
. . .
modelBuilder.Entity<Account>()
.HasMany(a => a.Persons)
.WithMany()
.Map(x =>
{
x.MapLeftKey("AccountId"); // <-- Account.Id to AccountsToPersons.AccountId??
x.MapRightKey("PersonId"); // <-- Person.Id to AccountsToPersons.PersonId??
x.ToTable("AccountsToPersons");
});
運行基本的Linq To EF Query時(from x in context.Accounts select x).ToList();
,查詢失敗,出現以下錯誤:
但是在運行Query時(from x in context.Persons select x).ToList();
,我沒有錯。
除了基本類型列之外,我的模型還添加了以下內容:
// In my Account Model, I also have this property:
public IList<Person> Persons { get; set; }
// In my Person Model, I also have this property:
public IList<Account> Accounts { get; set; } // <-- in the Person model
請注意,即使我的帳戶查詢通過並具有字段信息,但Persons
字段始終為null,即使我確定AccountsToPersons
表中有鏈接。
嘗試將p => p.Accounts
添加到WithMany
子句:
modelBuilder.Entity<Account>()
.HasMany(a => a.Persons)
.WithMany(p => p.Accounts) // <-- I think this should fix it
.Map(x =>
{
x.MapLeftKey("AccountId"); // <-- Account.Id to AccountsToPersons.AccountId??
x.MapRightKey("PersonId"); // <-- Person.Id to AccountsToPersons.PersonId??
x.ToTable("AccountsToPersons");
});
我剛為你的問題建立了一個測試解決方案,對我來說它看起來很有效。
我看到你做的一件事與我不同的是:
public IList<Person> Persons { get; set; } // <-- in the Account model
public IList<Account> Accounts { get; set; } // <-- in the Person model
嘗試修改為:
public DbSet<Person> Persons { get; set; }
public DbSet<Account> Accounts { get; set; }
如果這不起作用,我將發布我的整個設置。
我的結構看起來像這樣,它適用於您顯示的查詢:
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public IList<Account> Accounts { get; set; }
}
public class Account
{
public int ID { get; set; }
public string Name { get; set; }
public IList<Person> Persons { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Account> AccountSet { get; set; }
public DbSet<Person> PersonSet { get; set; }
public ApplicationDbContext()
: base("DefaultConnection")
{
this.Database.Log = (msg) => { Debug.Write(msg); };
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Account>().HasKey(x => x.ID);
modelBuilder.Entity<Person>().HasKey(x => x.ID);
modelBuilder.Entity<Account>()
.HasMany(a => a.Persons)
.WithMany()
.Map(w =>
{
w.MapLeftKey("AccountId");
w.MapRightKey("PersonId");
w.ToTable("AccountsToPersons");
});
}
}
您是否嘗試過輕微修改AccountsToPersons映射:
modelBuilder.Entity<Account>()
.HasMany(a => a.Persons)
.WithMany(p => p.Accounts) <-- Change
.Map(x =>
{
x.MapLeftKey("AccountId"); // <-- Account.Id to AccountsToPersons.AccountId??
x.MapRightKey("PersonId"); // <-- Person.Id to AccountsToPersons.PersonId??
x.ToTable("AccountsToPersons");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.