简体   繁体   English

两个相同表的LINQ TO SQL映射

[英]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: 所以图像我有两个ORM模型,就像这样一个:

[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) 发生主要问题时,当我需要通过表MySameTable1中包含的数据填充第二个表( MySameORM_Model2 )时(有时不是直接插入,有时不是)

So to not foreach all entries from MySameTable1 , I have tried this steps: 因此,为了不从MySameTable1中获取所有条目,我尝试了以下步骤:

1.Abstract class that contains all properties: 1.Abstract类,包含所有属性:

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. 也许它需要其他映射,但是Google并没有帮助我。

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. 等待存储过程来处理它,因此数据永远不必离开数据服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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