如何使用实体框架核心将SQL数据从一个表传输到另一个表 [英] How to transfer SQL data from one table to another using entity framework core

查看:61
本文介绍了如何使用实体框架核心将SQL数据从一个表传输到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有两个包含相同列的表。 TBL一和TBL两个是历史表。当我在TBL 1中输入数据时,我想使用EF Core 2.2将所有数据从TBL 1移动到Tbl 2(作为历史数据)

我不想写不必要的循环来制作代码难看。







我只需要复制表1中的SQL数据表2使用EF并避免循环。任何有映射器或任何有效的例子都会有所帮助。





我尝试了什么:



 var MyEntity = new MyEntities(); 
var TBL1 = MyEntity.TBL1.Find();
var TBL2 = new TBL2();

TBL2.CurrentValues.SetValues(TBL1);
//代码中不接受CurrentValues。给我构建错误

MyEntity.TB2.Add(data2);
MyEntity.TB1.Remove(data1);
MyEntity.SaveChanges();

解决方案

编写一个存储过程,将数据添加到两个表中,或者设置一个触发器。


Quote:

如何使用实体框架核心将SQL数据从一个表传输到另一个表



错误的方法!

你描述的是:

从SQL server =>中读取表1将数据发送到PC =>将数据发送回SQL server =>将数据写入SQL服务器上的表2。

,当它可以简化为:

从SQL server =>中读取表1 将数据发送到PC =>将数据发送回SQL server => 将数据写入SQL服务器上的表2.

和:

从SQL server =>中读取表1将数据写入SQL服务器上的表2.



你必须写1个SQL请求!

优势,它更快。


 have two tables in my database that contains the same columns. TBL one and TBL two which is history table. When I am entering Data in TBL 1 then I want to move All data from TBL 1 to Tbl 2 (as history Data) using EF Core 2.2

I don't want to write unnecessary loops to make the code ugly.




All I need is copy SQL Data from table 1 to table 2 using EF and avoiding loops. Any example with mapper or anything which works will help.



What I have tried:

var MyEntity = new MyEntities();
var TBL1 = MyEntity.TBL1.Find();
var TBL2 = new TBL2();

TBL2.CurrentValues.SetValues(TBL1); 
//CurrentValues is not accept in code. Giving me build error

MyEntity.TB2.Add(data2);
MyEntity.TB1.Remove(data1);
MyEntity.SaveChanges();

解决方案

Write a stored proc that adds the data to both tables, or set up a trigger.


Quote:

How to transfer SQL data from one table to another using entity framework core


Wrong approach!
What you describe is:
Read table 1 from SQL server => Send data to PC => Send data back to SQL server => Write data to table 2 on SQL server.
when it can be simplified to:
Read table 1 from SQL server => Send data to PC => Send data back to SQL server => Write data to table 2 on SQL server.
and:
Read table 1 from SQL server => Write data to table 2 on SQL server.

You have to write 1 SQL request!
Advantage, it is faster.


这篇关于如何使用实体框架核心将SQL数据从一个表传输到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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