Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapping multiple tables to a single entity class in entity framework

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.

like image 864
Chris McGrath Avatar asked Jul 12 '11 20:07

Chris McGrath


People also ask

How do you map an entity to multiple tables?

Yes, you can map an entity to 2 database tables in 2 simple steps: You need to annotate your entity with JPA's @Table and @SecondaryTable annotations and provide the names of the first and second table as the value of the name parameters.

Can one entity have multiple tables?

Yes, this is possible. You can specify your tables name and prefix them in a single entity class. You can use this piece of code. I want to insert the records only in 1 table i.e. if the financial year is 2021 then the records should be inserted in 2021 table and not in 2020.


1 Answers

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

like image 123
Eranga Avatar answered Sep 19 '22 23:09

Eranga