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.
Related
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!
I have created a new asp.net 2012 mvc application. I am moving an existing web forms application over to the mvc model. The web forms application uses linq to sql, but since it's no longer going to be supported, I am going to use linq to entities.
Having never used mvc or linq to entities, I have been reading every article I can find.
I added the ado.net entity model and generated the diagram from my existing sql server 2005 database. I was having problems in the .edmx diagram with a relationship so I deleted the view expecting to be able to drag it back to the diagram.
I have looked all over on adding an existing database view/table to a diagram with no luck. I don't want to regenerate it, I spent a lot of time modifying relationships etc. in the current Diagram.
How do I add back a view/table from a database to an existing diagram?
Thank you,
I was afraid the update model from database option would change what I had done in the model, so I avoided it.
After hours of searching, I decided to try it and duh... it let me pick what I want to add.
Right click on your .edmx file and select Update Model From Database. Check what you want and that's it.
I have a database name CostDB on a SQLserver. I need to create a page (ASP.Net MVC 4) with a simple dropdown list (i.e. Company Name) which will be directly bind to the Company column of DESE table from the CostDB database.
I am lost after looking at all tutorials and ASP.Net MVC4's own tutorials that what way to start? There are different ways of code first, model first etc. I already have the database. Do I need to create a Model first in this case? If so does it have to be exact name match from the DB table names? Or do I generate the model from the database?Which functions would I need to create if I need to show data in the dropdownlist on page load? Most importantly what is the order to create an MVC application for an existing DB. Among M,V,C, what to create first?
Completely new to ASP.Net MVC 4. Already did go through MVCMovies tutorial but with no luck. Any suggestion would be really appreciated.
I think the best answer is this: Whatever you are comfortable with and what works for you. There are tons of different approaches to development, and variations of each of those approaches. I tend to lean towards database first, as I don't have as much experience in code first.
Since you already have a database created, I think the easiest approach for your method would be to generate an EDMX off of your existing structure and go from there. Once you have the EDMX built and all the appropriate files referenced, it should be easy. Just populate a list from your EF model, toss it into the ViewBag, and then populate your control(s).
Here is a good link on how to do this. Populate #Html.DropDownList with a List using MVC
I would highly recommend PluralSight for learning any type of development, one of the best sites I have ever used. Best of luck!
Quick question about database views. Am I right in assuming that I can create a database of view of various tables and connect them how I want etc and then when I do queries, add, edit delete etc then MVC will figure it all out for me without needing to do any complex SQL in the controller or repository?
Odd question but just wanted to make sure my assumption was valid. Cheers
Unfortunately, MVC will not figure it all out for you, you'll still need to write the SQL code (or use an ORM framework) to communicate with the database.
What MVC gives you with it's architecture is a clear separation of responsibilities:
Views are responsible for displaying data and should be as simple as possible (i.e. little to no logic in them)
Model(s) contain the business logic and rules
Controllers are responsible for passing data between the Model and the Views.
What you are looking for is Scaffolding. In .net MVC I can't think of any tools which do this for you directly against the database. They all require either as Russ said an ORM i.e. Linq To SQL or Entity Framework (EF).
http://msdn.microsoft.com/en-us/library/cc488540.aspx
The closest you could get would be to use Database First model generation and then put the necessary MVC templates/views/code on top.
A database view is read-only so you will not be able to perform write operations on the view. You can however create a model from a view and display your data as defined from the view. If you are using an ORM solution such as ADO.NET Entities you can instantiate an object and add the child objects to it and be able to save the final result in a single transaction as well.
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.