在实体框架映射多个表到一个单一的实体类 [英] mapping multiple tables to a single entity class in entity framework
问题描述
在这之前被标记为重复我已经检查了其他相关的职位,他们不回答我的问题。
before this is marked as a duplicate i have checked the other related posts and they do not answer my question
我工作有2个表具有1的传统数据库:1的关系结果
目前,我有一种类型(1Test:1Result)每个定义的这些表
我想这些特定的表合并成一个单一类
i am working on a legacy database that has 2 tables that have a 1:1 relationship
currently i have one type(1Test:1Result) for each of these tables defined
i would like to merge these particular tables into a single class
目前各类看起来像这样
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; }
}
我想preFER他们看起来像这样
i would prefer them to look like this
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; }
}
我目前用流利的API,这些映射到我们原有的Oracle数据库
i am currently using the fluent API for mapping these to our legacy oracle database
这将是这些组合成一个单一的类的最佳方法
请注意,这是一个传统的数据库更改表不是一个选项,创建视图是不是在这一点上在项目可行的解决方案。
what would be the best method of combining these into a single class please note this is a legacy database changing the tables is not an option and creating views is not a viable solution at this point in the project
推荐答案
您可以用实体分割来实现这一点,如果你有两个表中相同的主键。
You can use Entity Splitting to achieve this if you have the same primary key in both tables.
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");
});
下面是
这篇关于在实体框架映射多个表到一个单一的实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!