使用 Entity Framework 4.1 Fluent API 在非主键字段上创建关联
我们使用 EF 4.1 和 Fluent API 从旧数据库(我们不允许更改)中获取数据。我们在创建两个表之间的关系时遇到问题,其中相关列不是主键和外键。
使用下面的类,我们如何配置 Report
和 RunStat
之间的一对多关系,以便 Report.RunStats
将返回所有ReportCode
字段相等的 RunStat
实体?
public class Report
{
[Key]
public int ReportKey { get; set; }
public string Name { get; set; }
public int ReportCode { get; set; } // Can we associate on this field?
public virtual ICollection<RunStat> RunStats { get; set; }
}
public class RunStat
{
[Key]
public int RunStatKey { get; set; }
public int ReportCode { get; set; }
public DateTime RunDate { get; set; }
}
基本上,我想使用 Fluent API 来配置 EF,使其将 Report.ReportCode
视为外键,将 RunStat.ReportCode
视为主键。
We are using EF 4.1 and the fluent API to get data from a legacy database (that we are not permitted to change). We are having a problem creating a relationship between two tables where the related columns are not primary and foreign keys.
With the classes below, how would we configure the one-to-many relationship between Report
and RunStat
such that Report.RunStats
would return all of the RunStat
entities where the ReportCode
fields are equal?
public class Report
{
[Key]
public int ReportKey { get; set; }
public string Name { get; set; }
public int ReportCode { get; set; } // Can we associate on this field?
public virtual ICollection<RunStat> RunStats { get; set; }
}
public class RunStat
{
[Key]
public int RunStatKey { get; set; }
public int ReportCode { get; set; }
public DateTime RunDate { get; set; }
}
Basically, I want to use the Fluent API to configure EF such that it considers Report.ReportCode
to be the foreign key and RunStat.ReportCode
to be the primary key.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的。 EF 中的关系遵循与数据库中完全相同的规则。这意味着主表必须具有被从属表引用的唯一标识符。对于数据库,标识符可以是主键或主表的唯一列。否则,它不是有效的关系。
实体框架不支持唯一键。如果您希望在
Report
和RunStat
之间建立一对多关系,您的依赖表 (RunStat
) 必须包含值为的列>Report.ReportKey
。没有其他方法可以使其自动 - 否则您必须简单地使其成为自定义属性并在需要时从实体框架手动填充它。It is not possible. Relations in EF follows exactly same rules as in the database. It means that principal table must have unique identifier which is referenced by dependent table. In case of database the identifier can be either primary key or unique column(s) of principal table. Otherwise it is not valid relation.
Entity framework doesn't support unique keys. If you want to have one-to-many relation between
Report
andRunStat
your dependent table (RunStat
) must contains column with value ofReport.ReportKey
. There is no other way to make it automatic - otherwise you must simply make it custom property and fill it from entity framework manually when you need it.此功能现在可以在 EF Core 1.0 (EF7) 中实现,如 @Brian 于 2014 年 7 月 16 日至 a 功能请求发布到 Microsoft 的 UserVoice 论坛。
因此,如果这些信息在引用的页面上消失,这些有价值的信息也不会丢失,以下是功能请求的文本:
微软宣布在 EF Core 1.0 中实现此功能:
This capability is now possible in EF Core 1.0 (EF7) as indicated in the reference provided by @Brian on Jul 16, 2014 to a feature request posted to Microsoft's UserVoice forum.
So that this valuable information is not lost should this information disappear at the referenced page, here is the text of the feature request:
And Microsoft's announcement of the implementation of this capability in EF Core 1.0: