主体密钥不是主键的关系出错 - ef6

时间:2015-01-27 15:59:38

标签: c# mysql entity-framework ef-code-first

这些是我的C#类,我使用MySql和EF:

---表---

int ID //(AI,PK)
Collection<C> cList

--- B表---

int ID // (AI, NN, UNIQUE)
int B1 //(pk)
int B2 //(pk)
Collection<C> cList

--- C表---

int ID //(AI, NN, UNIQUE)

int A_ID //(pk) - table A, field ID
virtual <A> objectA

int B_ID //(pk) - table b, field ID
virtual <B> objectB

表C映射:

HasKey(o => new { A_ID = o.A_ID, B_ID = o.B_ID });
            Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(x => x.A_ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            Property(x => x.B_ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

HasRequired(x => x.objectA).WithMany(x => x.cList).HasForeignKey(x => x.A_ID);//works
HasRequired(x => x.objectB).WithMany(x => x.cList).HasForeignKey(x => x.B_ID);//FAILS

我疯了,谷歌搜索,谷歌搜索和谷歌搜索......请帮忙!

0 个答案:

没有答案