Solved

entity framework code first and another database

Posted on 2013-01-12
3
450 Views
Last Modified: 2013-01-18
I have an issue with a code first database.  I have some data in a table that I copied to a new database so when I regenerate my code first db that I can copy it back.

If I create my code first db i can do inserts.  If I create my db, delete a table, then restore it using a select into statement, I can no longer do inserts without having an issue with null ids (they are generated).

How can I copy data from one database table to another without deleting the first?

I am currently trying this:
SELECT * into   [JTradeAdmin].[dbo].[OptionSettlementValues] FROM [JTradeAdminCopy].[dbo].[OptionSettlementValues]

but it requires me to delete the table first which gives me issues.
0
Comment
Question by:jackjohnson44
3 Comments
 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 250 total points
ID: 38771026
Do this:

truncate table  [JTradeAdmin].[dbo].[OptionSettlementValues]
insert into  [JTradeAdmin].[dbo].[OptionSettlementValues] select * from  [JTradeAdminCopy].[dbo].[OptionSettlementValues]

(Assuming you can perform DDL statements from C#)

If it's not a huge amount of data, try this instead of the truncate :

delete from  [JTradeAdminCopy].[dbo].[OptionSettlementValues]
0
 
LVL 25

Accepted Solution

by:
TempDBA earned 250 total points
ID: 38771094
Before doing it, you can add if exists, drop table condition

if Object_ID('[JTradeAdmin].[dbo].[OptionSettlementValues]') is not null
              Drop table  [JTradeAdmin].[dbo].[OptionSettlementValues]

SELECT * into   [JTradeAdmin].[dbo].[OptionSettlementValues] FROM [JTradeAdminCopy].[dbo].[OptionSettlementValues]
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 38771152
Hi jackjohnson44;

Entity Framework supports seeding the tables with data from your code in the database initialization process. Please see this post, Entity Framework Code First Database Creation Strategies, it has a sample to follow.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question