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

发布于 2024-11-29 03:10:59 字数 812 浏览 0 评论 0原文

我们使用 EF 4.1 和 Fluent API 从旧数据库(我们不允许更改)中获取数据。我们在创建两个表之间的关系时遇到问题,其中相关列不是主键和外键。

使用下面的类,我们如何配置 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; }
}

基本上,我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡间太子 2024-12-06 03:10:59

这是不可能的。 EF 中的关系遵循与数据库中完全相同的规则。这意味着主表必须具有被从属表引用的唯一标识符。对于数据库,标识符可以是主键或主表的唯一列。否则,它不是有效的关系。

实体框架不支持唯一键。如果您希望在 ReportRunStat 之间建立一对多关系,您的依赖表 (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 and RunStat your dependent table (RunStat) must contains column with value of 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.

沙与沫 2024-12-06 03:10:59

此功能现在可以在 EF Core 1.0 (EF7) 中实现,如 @Brian 于 2014 年 7 月 16 日至 a 功能请求发布到 Microsoft 的 UserVoice 论坛

因此,如果这些信息在引用的页面上消失,这些有价值的信息也不会丢失,以下是功能请求的文本:

唯一约束(即候选键)支持
(由 Kati Iceva 于 2010 年 9 月 10 日发布)

SQL Server 和其他数据库支持表上的唯一约束。
外键约束通常基于唯一约束
主方,主键只是一个特例
唯一的约束。实体框架目前只支持
基于主键的引用约束并且没有
唯一约束的概念。这个想法是:

  • 支持指定实体的唯一约束
  • 支持指定外键关联,该外键关联在主体端指定包含唯一约束的列,但
    不是主键。

微软宣布在 EF Core 1.0 中实现此功能:

已完成
(由 Diego Vega(Microsoft Entity Framework 项目经理)于 2016 年 8 月 9 日发布)

结束,因为 EF Core 1.0 中添加了对此功能的支持,我们
没有计划将其添加到 EF6 代码库中。另外,请创建
针对该功能的具体改进的新想法。

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:

Unique Constraint (i.e. Candidate Key) Support
(posted by Kati Iceva on Sep 10, 2010)

SQL Server and other databases support Unique Constraints on tables.
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:

Completed
(posted by Diego Vega (Program Manager, Microsoft Entity Framework) on Aug 9, 2016)

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. Also, please create
new ideas for specific improvements to the feature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文