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.
views:
290answers:
3Castle Project active record is a nice way of doing it.
If offers capabilities similar to ruby on rails active record.
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.
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:
- Your database keeps track of its version
- Every time you wish to change the schema, you write a small class to handle the upgrade (and, optionally, downgrade)
- Assign these classes an execution order
- 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.