[英]Create association on non-primary key fields with Entity Framework 4.1 Fluent API
We are using EF 4.1 and the fluent API to get data from a legacy database (that we are not permitted to change).我们正在使用 EF 4.1 和流利的 API 从遗留数据库中获取数据(我们不允许更改)。 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?使用下面的类,我们将如何配置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; }
}
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.基本上,我想使用 Fluent API 来配置 EF,使其将Report.ReportCode
视为外键,将RunStat.ReportCode
视为主键。
It is not possible.这不可能。 Relations in EF follows exactly same rules as in the database. EF 中的关系遵循与数据库中完全相同的规则。 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
and RunStat
your dependent table ( RunStat
) must contains column with value of Report.ReportKey
.如果您想在Report
和RunStat
之间建立一对多关系,您的依赖表 ( RunStat
) 必须包含值为Report.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.没有其他方法可以使其自动化 - 否则您必须简单地将其设为自定义属性并在需要时手动从实体框架中填充它。
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 .正如@Brian于 2014 年 7 月 16 日提供的对发布到 Microsoft 的 UserVoice 论坛的功能请求的参考中所示,此功能现在可以在EF Core 1.0 (EF7) 中实现。
So that this valuable information is not lost should this information disappear at the referenced page, here is the text of the feature request:因此,如果此信息在参考页面上消失,这些有价值的信息不会丢失,这里是功能请求的文本:
Unique Constraint (ie Candidate Key) Support唯一约束(即候选键)支持
(posted by Kati Iceva on Sep 10, 2010) (由 Kati Iceva 于 2010 年 9 月 10 日发布)SQL Server and other databases support Unique Constraints on tables. SQL 服务器和其他数据库支持对表的唯一约束。 Foreign key constraints are generally based on unique constraints on the principal side, with the Primary Key being only a special case of a unique constraint.外键约束通常基于主方的唯一约束,主键只是唯一约束的特例。 The Entity Framework currently only supports basing referential constraints on primary keys and does not have a notion of a unique constraint.实体框架目前仅支持基于主键的引用约束,并且没有唯一约束的概念。 The idea is to have:这个想法是:
- Support for specifying a unique constraint on an Entity支持在实体上指定唯一约束
- Support for specifying a foreign key associations that on the principal end specify columns(s) that comprise a unique constraint but are not the primary key.支持指定在主体端指定包含唯一约束但不是主键的列的外键关联。
And Microsoft's announcement of the implementation of this capability in EF Core 1.0:以及微软宣布在 EF Core 1.0 中实现这一能力的公告:
Completed完全的
(posted by Diego Vega (Program Manager, Microsoft Entity Framework) on Aug 9, 2016) (由 Diego Vega(Microsoft Entity Framework 项目经理)于 2016 年 8 月 9 日发布)Closing as support for this feature was added in EF Core 1.0 and we don't have plans to add it in the EF6 codebase.在 EF Core 1.0 中添加了对该功能的支持,我们没有计划将其添加到 EF6 代码库中。 Also, please create new ideas for specific improvements to the feature.此外,请为该功能的具体改进创造新的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.