简体   繁体   中英

Two same table's mapping LINQ TO SQL

For some reason, i have two tables(they are called differently) in my database and both are fully the same. Also, each of them has too many attributes.


So image i have two ORM Models,like this one:

[Table(Name = "DataHelper")]
class MySameTable1
{ 
  [Column(IsPrimaryKey = true)]
  public int Id { get; set; }
  [Column]
  public string Name { get; set; }
  [Column]
  public string Surname { get;set; }
  [Column]
  public string Country { get;set; }
  //etc. too much properties
}  

and the second one

[Table(Name = "DataSource")]
class MySameTable2
{ 
  [Column(IsPrimaryKey = true)]
  public int Id { get; set; }
  [Column]
  public string Name { get; set; }
  [Column]
  public string Surname { get;set; }
  [Column]
  public string Country { get;set; }
  //etc. too much properties
}  

So when i'm doing the job with table's:

DataContext _DataContext = new DataContext("connectionstring");

var MySameORM_Model1 = _DataContext.GetTable<MySameTable1>();  

var MySameORM_Model2 = _DataContext.GetTable<MySameTable2>();    

the main problem occurs,when i need to populate second table( MySameORM_Model2 ) via data that contains in table MySameTable1 (not sometimes it will be directly insert,but sometimes not)

So to not foreach all entries from MySameTable1 , I have tried this steps:

1.Abstract class that contains all properties:

public abstract class BaseSameTable
{
  [Column(IsPrimaryKey = true)]
  public int Id { get; set; }
  [Column]
  public string Name { get; set; }
  [Column]
  public string Surname { get;set; }
  [Column]
  public string Country { get;set; }
  //etc. too much properties
}
//inheritance
[Table(Name = "DataHelper")]
class MySameTable1 : BaseSameTable
{ }  
//same
[Table(Name = "DataSource")]
class MySameTable2 : BaseSameTable
{ }  

And it didn't work, I got strange exceptions with hierarchy submission

After this, i have changed abstract class to interface abstraction, but unfortenuly it didn't make the trick.

public interface IBaseEntity
    {
      int Id { get; set; }

      string Name { get; set; }

      string Surname { get;set; }

      string Country { get;set; }
      //etc. too much properties
    }

[Table(Name = "DataHelper")]
class MySameTable1 : IBaseEntity
{ 
  [Column(IsPrimaryKey = true)]
  public int Id { get; set; }
  [Column]
  public string Name { get; set; }
  [Column]
  public string Surname { get;set; }
  [Column]
  public string Country { get;set; }
  //etc. too much properties
}  

[Table(Name = "DataSource")]
class MySameTable2 : IBaseEntity
{ 
  [Column(IsPrimaryKey = true)]
  public int Id { get; set; }
  [Column]
  public string Name { get; set; }
  [Column]
  public string Surname { get;set; }
  [Column]
  public string Country { get;set; }
  //etc. too much properties
}    

So what is the right approach for my case and what can I do to achieve my goal? Maybe it needs additional mappings, but google didn't help me.

If your data is going from one table to another table, why get your host machine involve at all? Wite a stored procedure to handle it, so the data never has to leave the dataserver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM