Creating Models in ASP.NET MVC - 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.

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.

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.

Castle Project active record is a nice way of doing it.
If offers capabilities similar to ruby on rails active record.

Related

Is there a simple way to add a model in ASP.NET Core MVC via the GUI, besides the package manager console?

At work, I use ASP.NET MVC (not .NET Core) with Entity Framework and SQL Server. However, I wanted to practice a bit with ASP.NET Core MVC and some other SQL Server stuff (like experimenting with SSIS) at home, so I set up a small dev environment for me to work in. I created my database and tables, populated it with information, and created the default template you get with ASP.NET Core MVC. No issues so far!
But then, when I went to add a model into my project, I had no option to add an ADO.NET Entity Data Model like I do at work. After googling around a bit, I saw a mention that you could not create these kinds of models in .NET Core applications: https://learn.microsoft.com/en-us/answers/questions/357012/can39t-find-adonet-entity-data-model-missing-visua.html. I did some more googling, and it seems like just about every single thing I'm finding online is that you have to use package manager console, and type it all out.
Is this really the only way? Surely there has to be something better... It was very nice in ASP.NET MVC when I could add the model, and then it would take me through a wizard to get everything set up. I could create a new connection string, test the connection to the database, select which tables/views I wanted to add to the model, and I was all set! It was just as nice being able to go into my model and easily update the model with new tables, columns, or anything else I needed.
I get that code-first solutions are more mainstream these days, but I want to stick with the database first approach. Is there a simple, user-friendly (non package-manager console) approach for me to add models like I used to, but still get the benefits of .NET Core? Any good tutorials out there to get me on the right track (that aren't code first)? I appreciate anyone who can point me in the right direction!

Creating model from ado.net code

Is there a way to create a model from an ado.net SqlDataReader? I would really prefer to use Asp.net MVC 5 coming from Ruby on Rails rather than Webforms, but I need a way to display the data from an existing database on the view. Or could I possibly do this without creating a model by handling this in the controller? I don't want to create a model based on column names in the table in case the table (or db schema) changes later on.
Use Entity Framework`s DbContext.
Looking around, the normal solution would be to reverse-engineer the model from the database found here: http://www.codeproject.com/Articles/671590/Reverse-Engineering-an-Existing-Database-in-your-A
Unfortunately, this is something that usually has to be done through Visual Studio, which I will not always have access to once the application is in production mode. If the schema changes, I would not be able to use the Update Model from Database... command in VS 2013. Therefore, I'll have to use Asp.net Web Forms for my Database-First application.

MVC web framework that allows end users to define models at runtime

It seems this can be hacked into Django, but I'd rather prefer a framework that has better support for end user defined models.
Basically, I want the users of my app/website to be able to do at the runtime of the application what I do at compile time when writing the Model code: specify models that generate/modify a database schema. Obviously I cannot let the users of the webApp modify the code in models.py, so there has to be another way. Concurrency shouldn't be an issue, since each user-defined model would belong to only one user.
I don't mind using any programming language (Python, Haskell, JavaScript etc.) or any specific database SQL, NoSQL, whatever. Rails/Django freed me from writing a lot of repetitive code, now I simply want that functionality of modifying the model also at runtime (and preferably the corresponding views and controllers). If there is a good framework that rids me of writing all that code then I'll use it.
If there's no framework supporting it natively, does someone know a framework that at least makes it easy?
Portofino version 3 (http://www.manydesigns.com/en/portofino/portofino3) allows a modeler user to create data models interactively using a web interface called the "upstairs level". The system automatically generates a user interface (CRUD, charts, workflows) based on the model definition, without recompiling and basically in real-time with model changes.
You can check the reference manual to see what kind of models are supported:
http://www.manydesigns.com/en/portofino/portofino3/3_1_x/reference-manual
Currently Portofino 3 is an end-of-life version. The newer version 4 (http://www.manydesigns.com/en/portofino) is a significant re-write that currently does not support online editing of the data model as version 3 did, but keeps the same principle of making the application editable (through admin/configuration pages) and customizable (using Groovy) online without recompiling or restarting the server.
For data-model changes and db refactoring, Portofino 4 relies on Liquibase:
http://www.liquibase.org/

Creating asp.net membership tables

I am using Entity Framework code first, so keep dropping and re-creating the database. My asp.net membership tables keep disappearing as well - particularly annoying on the build server.
The best way I have found to create the tables so far involves a post build event running aspnet_sql.exe, but the connection strings need to be hard-coded and its making staging-release environments difficult.
Is there an elegant way to create the tables in code?
Have a look at Universal Providers. Then you don't need to use aspnet_sql.exe.
Also use migrations rather than drop and re-create database.
Why are you dropping and re-creating tables so often? Maybe migrations could solve this issue for you.

asp.net mvc model database change

I've used mvcScaffolding and mvc3 to generate my tables in a sql2008 database. some data has been added.
I've changed a single model and wish to alter the underlying table.
is there a way to auto-update a single table to keep it in sync with the model?
after adding a new model how can the database table be created without recreating the database
You're looking for something like Rails' migrations. There are a couple .NET migrations providers floating around, but the basic setup you've described doesn't support alterations to the schema without recreating the database (at least in my experience).
have you tried treating your database an an existing one as described in this example by Scott Gu?
http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx
Hopefully thias way you will not have to re-create your db everytime a change is required...
Regards
Paul

Resources