2

Hopefully what I'm after is possible at all. I'm currently looking at a way to do what Entity Framework Code First migrations does when you use Package Manager Console in Visual Studio but do it in runtime. The two processes I'm after are Add-Migration and Update-Database.

To explain a bit more, I want to be able to construct custom meta data for the Model and compare that against existing or non-existing database. This should output the same migration file that you get from Add-Migration command in Package Manager console.

After I've got one or more migration files I want to run the Update-Database command again in run-time, programatically to apply Up / Down changes to the database.

The key point is to be able to provide a completely custom (virtual?) model meta data to the Add-Migration procedure, not one based on actual poco classes.

If anyone has done something similar before, I'd appreciate any pointers on where to look as I didn't have much luck with Googling / EF documentation.

EDIT:

To address the WHY: I want my users to be able to define their structures in run-time. They would define the structure as part of a changeset and then be able to apply one or more changesets to the underlying database. Essentially, users would do from web-ui what a developer does from visual studio, except that the classes won't be .cs files but instead a class description in a relational database.

EF seems like a perfect fit to do this because it already does everything I need it to do. I've done schema comparison and run-time change mechanisms in the past. Since EF already does this, I don't want to reinvent the wheel. The migration files that EF Code First outputs are also a perfect fit fro what I'm trying to do.

The reason I haven't yet dug into the EF source is I wanted to check if someone else has attempted this before and if they can share and pointers. Some concrete questions:

What class / method do I need to be able to run schema compare?

How do I hook into that process to supply my own meta-data for "POCO" classes?

What are the options for outputting the detected schema changes (the Up, Down and Seed methods)? Ideally I'd like to compile a change set into a satellite assembly when user chooses to apply changes.

How can one run DbMigration instances in run-time?

I'm after some concrete examples.

Ales Potocnik Hahonina
  • 2,977
  • 2
  • 26
  • 32
  • the 'why' would help (your 4th paragraph maybe?), that's not what you usually may need, migrations code is always done manually (as you change the 'code' and then generate), then automatically 'applied' based on the the Db state. So there're multiple angles here (code, Db, migration tables). I think you should look into the code (EF-6 is in plain text), check how migrations are applied, synced. How model metadata is generated (from code/pocos), is complex, you don't want to be doing that yourself - but source code is your best friend, it seems you're pretty deep, you need to dive in – NSGaga-mostly-inactive Jul 15 '14 at 16:19
  • 1
    Please see my answer on [Enable Entity Framework migrations in Mono](http://stackoverflow.com/a/20382226/475031). – bricelam Jul 15 '14 at 16:27
  • I've updated the question to address the "why". Thanks bricelam, for the link to your answer but I'm afraid that's not what I'm after. – Ales Potocnik Hahonina Jul 16 '14 at 10:43
  • Did you ever get this to work? – Tom Oct 19 '15 at 13:25
  • Not really sadly. Was digging through EF source code online but that didn't really turn anything up. Ended up going with a different (more manual) approach. – Ales Potocnik Hahonina Oct 20 '15 at 12:15

0 Answers0