Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Models in ASP.NET MVC

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.

like image 927
tghw Avatar asked May 11 '09 17:05

tghw


People also ask

Why do we create models in MVC?

An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic.

What are the three models of MVC?

In fact, in ASP.NET MVC, there are three distinct types of model: the domain model, view model and input model.

What is model in MVC with example?

The model contains all the data-related logic that the user works with, like the schemas and interfaces of a project, the databases, and their fields. For example, a customer object will retrieve the customer information from the database, manipulate or update their record in the database, or use it to render data.

What is model in asp net core MVC?

A model is a class that contains the business logic of the application. It also used for accessing data from the database. The model class does not handle directly input from the browser.


1 Answers

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.

like image 184
Benjamin Pollack Avatar answered Sep 20 '22 10:09

Benjamin Pollack