【问题标题】:mapping multiple tables to a single entity class in entity framework将多个表映射到实体框架中的单个实体类
【发布时间】:2011-07-12 20:20:25
【问题描述】:

我正在处理具有 2 个具有 1:1 关系的表的旧数据库。 目前,我为这些表中的每一个定义了一种类型 (1Test:1Result) 我想将这些特定的表合并到一个类中。

目前的类型是这样的

public class Result 
{
    public          string      Id                  { get; set; }
    public          string      Name                { get; set; }
    public          string      Text                { get; set; }
    public          string      Units               { get; set; }
    public          bool        OutOfRange          { get; set; }
    public          string      Status              { get; set; }
    public          string      Minimum             { get; set; }
    public          string      Maximum             { get; set; }

    public virtual  Instrument  InstrumentUsed      { get; set; }

    public virtual  Test        ForTest             { get; set; }
}


public class Test
{
    public          int     Id            { get; set; }
    public          string  Status        { get; set; }
    public          string  Analysis      { get; set; }
    public          string  ComponentList { get; set; }
    public virtual  Sample  ForSample     { get; set; }
    public virtual  Result  TestResult    { get; set; }
}

我希望它们看起来像这样

public class TestResult
{
    public          int        Id              { get; set; }
    public          string     Status          { get; set; }
    public          string     Analysis        { get; set; }
    public          string     ComponentList   { get; set; }
    public          string     TestName        { get; set; }
    public          string     Text            { get; set; }
    public          string     Units           { get; set; }
    public          bool       OutOfRange      { get; set; }
    public          string     Status          { get; set; }
    public          string     Minimum         { get; set; }
    public          string     Maximum         { get; set; }

    public virtual  Instrument InstrumentUsed { get; set; }
}

我目前正在使用 fluent API 将这些映射到我们的旧版 Oracle 数据库。

将这些组合成一个类的最佳方法是什么? 请注意,这是一个遗留数据库。更改表格不是一种选择,并且在项目的这一点上创建视图不是一个可行的解决方案。

【问题讨论】:

    标签: c# entity-framework asp.net-mvc-3 c#-4.0 entity-framework-4.1


    【解决方案1】:

    如果您在两个表中具有相同的主键,则可以使用实体拆分来实现此目的。

      modelBuilder.Entity<TestResult>()
        .Map(m =>
          {
            m.Properties(t => new { t.Name, t.Text, t.Units /*other props*/ });
            m.ToTable("Result");
          })
        .Map(m =>
          {
            m.Properties(t => new { t.Status, t.Analysis /*other props*/});
            m.ToTable("Test");
          });
    

    这是一个有用的article

    【讨论】:

    • 他们确实有相同的密钥,所以如果他们不能通过单独映射关系仍然可能,或者在这个版本的框架中还不可用,这应该可以工作?
    • 如何映射到 Instrument?
    • EF Core 中 Map() 的等价物是什么
    • @KarthicG 没有。 EF Core 6 仍然不支持将多个表映射到单个实体 - 但如果您使用数据库优先(而不是代码优先),那么您可以使用 VIEW 将表连接在一起并欺骗 EF Core 和小指- 发誓VIEW 确实是TABLE - 这是完全有效的,因为您可以在SQL Server 中为VIEW 对象定义INSERTUPDATE 处理程序。
    猜你喜欢
    • 2015-08-17
    • 1970-01-01
    • 2013-02-18
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 2015-01-07
    相关资源
    最近更新 更多