Before this is marked as a duplicate, I have checked the other related posts and they do not answer my question.
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.
The current types look like this
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; }
}
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; }
}
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 that 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");
});
Here's a useful article