Add existing database view/table from object explorer to entity diagram - asp.net-mvc

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.

Related

How to modify CRUD operations with Entity Framework in .NET MVC application

I need to work out how to do CRUD stuff in an MVC application, passed to me by a former colleague (I don't have any other info, just the application and the database). So I can see there is Model1.edmx and the model browser that contains MyApp.Model>EntityTypes>MyTable representation, and MyAppModel.Store that contains a representation of the table, and the Model1.Designer.cs file which has methods that look like they must be CRUD related (e.g. OnLastNameChanging, OnLastNameChanged). I can run the application and insert and update records to a db table.
What I need to know is where / how do I code other CRUD operations and use Entity Framework to work with WHERE clauses, and do stuff like update another record in a table depending on the value of a given field in the record being inserted or updated.
I've worked with MVC on one other small project but haven't really worked with entities. I'm used to the WebForms / ADO.NET / stored procedures way of doing things.
Any help gratefully received.
Happy new year!
I guess you are using database first approach.
some useful links for Code first approach:
http://www.codeproject.com/Tips/793159/Code-First-Approach-in-Entity-Framework-using-Data
other for database first approach
http://www.aspdotnet-pools.com/2014/08/insert-update-delete-operation-in.html
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

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.

How to populate data on a dropdownlist from existing SQL DB ASP.Net MVC 4

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!

ASP.NET MVC Database Views

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.

Can EF4 generate POCO for me, or do I have to write them myself?

I've been playing about with the Entity 4 framework lately and it's pretty nifty. I've setup a class called Customer.cs with some properties like Name, Address etc. I also have a class called StoreEntities.cs which binds these back to the database through DbSet. It works fine and I can pull all my customers from the database.
The problem is every tutorial I come across on the internet generates their classes by hand. What I mean is, they all say something like "Now I'm going to make a new class called Orders with the following properties" and then proceed to write it out. That might be ok if I was starting a new project, but I'm porting over my old website so I have upwards of 20 tables in my database. If I go through and write all these out by hand I'm going to be there all week :D
Plus I'm not sure what would happen if I made some changes to the database (since I would have to go back through and update all my classes by hand).
I was hoping EF4 would have something similar to a Class Diagram, where I can point it at my database and have it generate a bunch of classes for me based on that.
I'm a little lost on this. Am I going about this the right way?
You should take a look at the ADO.NET C# POCO Entity Generator. With that you should be able to generate your model from your existing database and T4 templates will generate your POCO classes based on your databases tables, etc.
Here is a link to MSDN that explains how to Update Model from Database using the Update Model Wizard.
There are two ways to achieve what you want. Both start from adding new item to your project. In the "Add new item" window select "ADO.NET Entity Data Model". There you will have two options. One to generate your model from existing database and second to create model manualy in designer. If you choose first one you can simply drag and drop tables from server explorer to design surface and all your entities and their relations will be generated for you automaticaly. Also you can modify that generated model later.

Resources