Solved

entity framework code first and another database

Posted on 2013-01-12
3
455 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

831 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