Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...