I\'ve read about Code First Migrations but it seems that this is not really suited to the Enterprise.
We have a DBA that does all our Database changes and we don\'t nee
A bit late in replying, but here goes:
If you use the EF Power Tools extension for Visual Studio, it gives you the ability to do what Rowan Miller calls "Code Second". Take a look at this article.
It lets you point to an existing database and it will generate nice POCO classes and use DbContext
just like if you had done it via Code First. No more ObjectContext
or edmx
files. The fluent configuration is also fully generated for you.
Moving forward, the EF team will be rolling this feature into the main EF tooling so you don't have to download the EF Power Tools extension.
Usually in such cases people use Database First approach.
Writing entities code manually when someone already designed database for you and when you can generate or update the model with several clicks just makes no sense. Of course, Code First could be convenient if your team is familiar with some other ORM where it was main approach and is not quite familiar with Entity Framework yet, or if your team is extremely small and nobody can write SQL script, but if you have skilled DBA, why you could need Code First?
Few days ago i faced the same problem,
old database, the __MigrationHistory table is
made change in the database and recreated it at local machine
added ContextKey, Model and ProductVersion from the __MigrationHistory table of currently created database, to old database’s __MigrationHistory table.
oh don't forget to use alter scripts to old database. For more just check,
http://www.codeproject.com/Tips/800936/Entity-Framework-code-first-migrations-alternative
To me it doesn't seem like these other answers are sufficient.
You can turn off the EF initializer:
public ApplicationContext : DbContext
{
public ApplicationContext()
: base("ConnectionStringName")
{
Database.SetInitializer<ApplicationContext>(null);
}
// DbSets here
public DbSet<Part> Parts {get; set;}
// override OnModelCreating below ...
}
And then use Fluent API / data annotations however you normally would to setup your POCOs/models to match the existing DB.
Details at this blog: http://cpratt.co/entity-framework-code-first-with-existing-database/
In case that URL does not work in the future - here's what I mean:
After having set the Initializer off above, configure your POCO's that correspond to a table:
public class Part
{
public string PartID {get; set;}
public string Description {get; set;}
public decimal Weightlbs {get; set;}
public decimal Price {get; set;}
}
Then map your POCO to the existing DB table by overriding this method in your Application Context
class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Code First will assume a lot, but if you need to override things:
modelBuilder.Entity<Part>().ToTable("db_PartTable");
modelBuilder.Entity<Part>().Property(p => p.PartID)
.HasColumnName("Part_ID");
modelBuilder.Entity<Part>().Property(p => p.Description)
.HasMaxLength(100)
}
Another good blog for this, by Scott Guthrie: http://weblogs.asp.net/scottgu/using-ef-code-first-with-an-existing-database
Even though I use EF Code First style (as opposed to EDMX), I am still technically using a database first approach because I never let EF generate the database for me. Instead, I create the classes to model the database. This sounds like what you need to do in your case.
As for the DBA changing things.. whether you need to update your domain entity classes depends on what it is that the DBA is changing. For example, if he is increasing the length of a varchar(100)
to varchar(200)
or something like that, then that change shouldn't really break your code (but it's still recommended you update your code to match this anyway). If he's removing or renaming some columns though, then you would definitely need to update your domain entity classes because that would obviously cause an exception regarding the underlying model not being in sync.
Just to add a few thoughts on this - take a look at these two posts (I made a while ago):
https://stackoverflow.com/a/10164815/417747
https://stackoverflow.com/a/10255051/417747
In short, from my experience:
hope this helps a bit.