Solved

entity framework code first and another database

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

947 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

20 Experts available now in Live!

Get 1:1 Help Now