Entity Framework 6个特定的列映射

Entity Framework 6 specific column mapping

在SQL中,我会告诉数据库外键约束是什么。

但是 fluent EF6 显然没有办法让我指定绑定集合时要使用的列。

难道不能准确地告诉 DbModelBuilder 在哪个列上绑定关系?还是要求一直做主键?

Table_Person id int // pkey. Multiple people records UniqueID int // the unique person sometext varchar(256) // database therefore tracks changes to this, since unique person can have many records (pkeys).

Table_Address id int //pkey fk_unique int // should map to UniqueID of person, NOT the pkey. line1 varchar(512) state varchar(64) etc 一个独特的人有很多记录,他们的 uniqueID(不是 pkey)有很多关联地址。实际结构远比这复杂。但是我正在寻找一种从根本上做到这一点的方法......

非常希望在 Persons 模型中有一个 ICollection<Address> Addresses。但是要为代码优先迁移启用这样的东西……似乎不可能? 是的,我可以 Add-Migration 然后手动修改生成的 code/sql。但这不是打败了重点吗?或者这是常见的做法?

如果您能够修改数据库模式,您可以将人们的 UniqueID 放入他们自己的 table 中,命名为 "Person",并将现有的 table 重命名为 "PersonVersion" .然后在 "PersonVersion" 和 "Address" 上与新的 "Person" table 建立联系。最后,在您的应用程序代码中创建 Person、PersonVersion 和 Address 模型,EF 应该可以毫无问题地绑定。