views:

290

answers:

3

I'm just starting a project in ASP.Net MVC with LINQ to Entities and I was wondering if there was a nice, clean way of defining models that creates the appropriate tables in the database for me. I'm most familiar with Django (in terms of MVC frameworks) and am looking for the .Net equivalent of models.py so I can have everything versioned. Any ideas? It would be even better if it had some form of schema migration, a la django-evolution and the like.

A: 

Castle Project active record is a nice way of doing it.

If offers capabilities similar to ruby on rails active record.

Konstantinos
Castle Project uses NHibernate, not LINQ to Entities, no?
Benjamin Pollack
true, my bad, still it's a good alternative if you want migration-like functionality
Konstantinos
+1  A: 

Another option is to use NHibernate with FluentNhibernate which will auto map your model based on conventions. You can also override a mapping to tweak it for your needs.

Derek Ekins
+5  A: 

I think what you want to do is to turn the question around. Entities can be automatically generated from the database, so the issue is simply using a .NET mechanism to maintain your database schema. Since you're not using NHibernate, which these other solutions require, I would suggest using MigratorDotNet. MigratorDotNet uses exactly the same idea as Ruby on Rails migrations:

  1. Your database keeps track of its version
  2. Every time you wish to change the schema, you write a small class to handle the upgrade (and, optionally, downgrade)
  3. Assign these classes an execution order
  4. If the database is ever not up-to-date, simply execute the classes' upgrade methods in order

Since you'll only be regenerating your Entities at compile time, I'd recommend running the migration scripts, and then regenerating your entities, as a part of your build process. MigratorDotNet already comes with an MSBuildTarget, adding it will just involve a couple of clicks.

Benjamin Pollack