如何解决实体中的复制表?

编程入门 行业动态 更新时间:2024-10-20 00:29:40
本文介绍了如何解决实体中的复制表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

如何解决实体中的复制表?

本文发布于:2023-10-16 16:31:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   实体

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!