Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
 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();
Posted
Updated 20-Apr-19 17:04pm
Comments
[no name] 20-Apr-19 17:17pm    
Use SQL on the server; with some sort of "criteria" for moving / copying records.

Round-tripping this type of stuff is "not efficient".

Write a stored proc that adds the data to both tables, or set up a trigger.
 
Share this answer
 
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.
 
Share this answer
 
Comments
Kaps_ 22-Apr-19 13:09pm    
Good suggestion. how do we do this without writing Stored procedure? I am using EFcore with C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900