如何解决实体中的复制表? [英] How to solve copy table in entity?

查看:61
本文介绍了如何解决实体中的复制表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DB1  -  Table Name:STUDENT1

ID   NAME    BRANCHCODE COUNTRYCODE
21   Emily      02          001
26   Alex       56          002
35   Toms       89          003
48   Kelvin     47          004







DB2  -  Table Name:STUDENT2

ID   NAME       BRANCHCODE COUNTRYCODE
14   Mary          32          015
72   Michael       65          066







我想比较两个数据库,如表2中的可用和1个表中的非值。



我想添加到新表。我想做状态0.





我不喜欢知道如何更改数据库,并且由于比较,我无法将返回的值添加到新表中。






I want to compare two database and if Available in table 2 and Non-values at 1 table.

I want to add to the new table.And I want to do Status 0.


I don't know how to change the database and I couldn't add the returned value to the new table as a result of the comparison.

DB1 -  Table Name: NEWSTD

ID   NAME       BRANCHCODE COUNTRYCODE   Status
14   Mary          32          015          0
72   Michael       65          066          0





我的尝试:





What I have tried:

public static void Tables()
{
    var studentone=Context.Entities.Student1.Select(p=>new { p.BRANCHCODE,p.COUNTRYCODE});
    //change db code dont write.
    var studenttwo=Context.Entities.Student2.Select(p=>new { p.BRANCHCODE,p.COUNTRYCODE});
    var result=studentone.Intersect(studenttwo);

List<NEWSTD> main=new List<NEWSTD>();
foreach(var item in result)
{
  var item2=student2.SingleOrDefault(s=>s.BRANCHCODE==item.BRANCHCODE && s.COUNTRYCODE==item.COUNTRYCODE);
  if(item2 !=null)
{
     NEWSTD st=new NEWSTD
{
      BRANCHCODE=item2.BRANCHCODE,
      COUNTRYCODE=item2.COUNTRYCOD
};
mainbuild.Add(st);
}
}
DbContext.Entities.NEWSTD.AddRange(main);
DbContext.Entities.SaveChanges();
}

推荐答案

除非最近在EF中发生了根本性的变化,否则无法使用EF创建新的数据库表。
Unless something has fundamentally changed in EF lately, you can't create a new database table using EF.


为什么不在DB中创建存储过程并将其添加到Entity Framework,或通过ADO调用它?



我甚至会给你SP代码
Why don't you just create a Stored Procedure in the DB and add it to Entity Framework, or call it via ADO?

I'll even give you the SP code
     INSERT db2.dbo.NEWSTD (ID, NAME, BRANCHCODE, COUNTRYCODE)
     SELECT ID, NAME, BRANCHCODE, COUNTRYCODE, 0
     FROM   db2.dbo.STUDENT2
     WHERE  ID NOT IN (SELECT ID FROM db1.dbo.STUDENT1)
--   AND ID NOT IN (SELECT ID FROM db1.dbo.NEWSTD)

如果您取消注释,注释掉的最后一行将防止重复

The last line which is commented out will prevent duplicates if you un-comment it


这篇关于如何解决实体中的复制表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆