简体   繁体   English

使用 Entity Framework 4.1 Fluent API 在非主键字段上创建关联

[英]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?使用下面的类,我们将如何配置ReportRunStat之间的一对多关系,以便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 .如果您想在ReportRunStat之间建立一对多关系,您的依赖表 ( 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.

相关问题 使用非主键列的实体框架自引用 - Entity Framework Self Referencing Using Non-Primary Key Column 非主键上的实体框架关系 - Entity Framework Relationships on Non-Primary Keys 使用主表中的重命名字段和非主键创建实体关系 - Creating entity relationship with renamed fields and non-primary key in primary table 在实体框架中,在父实体中加载子级(非主键关联) - In Entity framework load child in parent entity (non primary key association) 是否可以在Entity Framework中的两个普通字段之间创建关联,从而忽略主键? - Is it possible to create an association between two normal fields in Entity Framework, thereby ignoring the primary key? 实体框架 - 如何在辅助表中的非主键列上连接表? - Entity Framework - How do I join tables on non-primary key columns in secondary tables? 实体框架多列作为 Fluent Api 的主键 - Entity Framework Multiple Column as Primary Key by Fluent Api 非主要唯一实体不可在实体框架数据库中首先连接 - Non-Primary Unique Not Joinable in Entity Framework Database First 用于查找具有非主键值的实体的存储库方法 - Repository method to find an entity with a non-Primary Key value Entity Framework Core 2.1.14 DB First Missing Foreign Key on a misconfigured data type 的非主候选键 - Entity Framework Core 2.1.14 DB First Missing Foreign Key on non-primary candidate key of a misconfigured data type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM