Solved

entity framework code first and another database

Posted on 2013-01-12
3
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

734 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