Solved

entity framework code first and another database

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now