0

Has anyone published a generic database initializer capable of taking an XML file (or other file representation) of database contents and using it to initialize a code-first database in the Seed method?

I'm thinking of something that uses Reflection to populate the database. I realize there is a lot of fun to be head with FK relationships, but if such a tool existed, especially if it was bi-directional, it would be very handy. Unfortunately my google-fu is failing me.

Just wanted to make sure I wasn't missing something...I've got an awful lot of init code I could throw away...

Andiih
  • 12,285
  • 10
  • 57
  • 88
  • 2
    Hmm.... this approach is specifically called **code-first**, e.g. you don't need any XML config or anything - you just code it ... seems a bit "off" in that context to want to introduce some config XML again... – marc_s Jul 02 '11 at 10:00
  • 1
    Its called code first. Its not called "Destroy your data every time you make a tiny schema change first". For some projects, that's not a problem. For others, a way of preserving data *while still writing code first and not swapping to schema first* would be a serious benefit. I do take your point about XML. I'd be happy with a data aware T4 template that auto generated EF insert code. – Andiih Jul 03 '11 at 20:25

1 Answers1

1

Are you talking about the actual data itself? In that case, XML and reflection would be overkill. Just put the data in the DB, and the re-export your INSERT statements:

What is the best way to auto-generate INSERT statements for a SQL Server table?

Then, in your Seed methods (or InitializeDatabase()), read in your exported file and use context.Database.ExecuteSqlCommand().

Community
  • 1
  • 1
anon
  • 4,578
  • 3
  • 35
  • 54