MVC database table relationships explained - c#

I was hoping someone would help explain some of MVC to me.
Say for example you have,
var orderDetails = new List<App_Orders>();
App_Orders is a table in the database that the application is using. So I say orderDetails is a List of type App_Orders?
In this example, say the user clicks on a link to open an existing Customer, on screen is listed all the Customers Orders. The code gets the Orders that are on screen...
List<App_Orders> ordersInSession;
.....
ordersInSession = (List<App_Orders>)Session["Orders" + id];
If my understanding is right of MVC and the way the tables in the database are joined, if a table has a relationship with App_Orders, it will be available when accessing ordersInSession. For example, App_Orders has a relationship with App_OrderItems and Ref_OrderType. An Order can have one or more Items and the Orders are of a particular Type. So when you type, ordersInSession. , intellisense will pop up with the list of properties of App_Orders, for example, OrderID, OrderDate, but will also have App_OrderItems and Ref_OrderType.
Am I understanding that right? If the table has relationships, then those tables are also available? If I am, would you call those attributes?
Sorry I'm not even sure that made sense, but if anyone has any idea what I mean if they know of a tutorial that I can read to help me understand this part of MVC better.
Thanks in advance

ASP.NET MVC is a framework for building web applications, it has "nothing to do" with databases and is not providing facilities to manipulate them.
The idea is that the user asks a Controller to show data (Model) on a "page" (View). Nothing to do with databases.
What you want probably is to add some code in your MVC Controllers so they build a Model from objects in a database. For this your can use an ORM framework like Microsoft's Entity Framework, or lightweight/easy things like Dapper or PetaPoco.
Your MVC controllers can then load data from the database, construct a list of App_Orders and give that as a Model to the View to display.
Avoid storing things in the Session as much as you can, it's not meant to hold volumes of data and it's asking for trouble e.g. if the browser opens multiple tabs in the browser.

Are you using any ORM framework?
For Entity Framework you can read about relationship here

Related

Updating Sql , through a view via wpf and ef

Background
I am starting a series of simple screens to display and update info in our ERP database.
I have worked through the wpf controls and understand the need for Observable Collections and after reading around on Entity Framework I understand the advantages of it sitting on top of ADO.net compared to the basic SQL methods and Datatables I am more comfortable with due to my SQL experience.
When I tried EF when I was first started working with Data CRUD screens I struggled to get the Observable Collections I needed, but having read this walk through last night ( https://msdn.microsoft.com/en-us/data/jj574514.aspx) and seen the notes for VS 2010 to edit EF code to get Observable Collections I think I want to try EF again.
Question
My Data screen needs present information which has be combined from five tables and a couple of sub views to be meaningful to the user.
Included in the dataview is a simple Y/N flag which comes from one of the five Datatables.
Can the user update the Y/N flag through the view mapped to EF, displayed in WPF datagrid ?
Or do I have to map all the base Datatables and sub views and recreate the view and Data Context in EF to allow the update to work?
If it is the latter does any one know of any tutorials or walk through I can use on my test development to try EF please
Thanks
The user can update a field via the view mapped to EF, however it is a little bit more complicated.
For a single table mapped to EF, the update is done by EF automatically, for a view mapped to EF you need to define the update function in the mapping details.
The function would be in form of an SQL stored procedure mapped to EF.

Create Entity Model of Data in Azure Asp.Net

I have created an ASP.NET Azure WebForms application. It has its default database and I can access it & modify AspNetUsers table using IdentityModel.cs (EntityFramework) as shown in many sites.
Now I have created other tables in the database namely "Projects", "customers", etc. and also have designed aspx forms based on those fields. Now I am not able to get, how to associate these forms wit the tables in the database. I wouldn't prefer Scafolding as many sites show, instead I would prefer something like IdentityModel does. I can have control to retrieve data, display, show, edit, add functions based on my button clicks or so.
I read many tutorials & blogs but couldn't get how to achieve this as I want. Tutorials shows using Scaffolding & MVC. I also looked to add "Entity Data Model" in projects, but none create .cs file; so again lost their. Can anyone please help me know how can I achieve what I am trying to.
Please any help is highly appreciated.
What I think you are looking for is the ability to reverse engineer a model from existing tables. This is possible, although can be a little painful if your existing database has a complex schema. If you are using Studio 2013 you can add a New Item of type ADO.NET Entity Data Model and on the screen that appears choose Code First from Database. You need to then point the wizard your db connection string. This will create the context classes, entity classes and configuration that you need.
More details are in this document from Microsoft.

How to save it in entity right

Assume i have a big entity and want to create a typical CRUD application. A user shouldn't have the ability to save some fields of my entity.
So i see two ways to implement change-save logic:
a)
Get entity from DB
Out to page with all fields(fields which user shoudnt change outed as hidden inputs)
Take entity by post method
Attach to context and save
In this case i need to out on page useless fields. And it is sucks no doubt.
b)
Get entity from DB
Out to page only necessary fields(fields which user can change)
Take entity by post method
Get entity from DB
Fill DB entity by new values and save
In this case i need to do additional query to DB. So it is not good for perfomance.
What is right way?
or C):
Get entity from DB
Map entity to ViewModel with only the allowed fields
Post ViewModel with data back to controller
Map ViewModel back to Entity
Attach and Save.
EDIT:
I highly recommend AutoMapper for the mapping to and fro
Interestingly enough, I just watched a video made by Julie Lerman in which she discusses almost the exact same problem. Neither of your solutions was what she went with:
Have a separate entity class that contains the fields that you want to go on the screen, but still maps to the same table that the regular one does. Then just query that DbSet for grabbing the entity (with only those fields), and save the updates to that.
She mentioned this while discussing implementing Domain Driven Design on top of Entity Framework. So that if you have different DbContexts for different functions in your application, you can still have a DbContext that you're using write to the table, but you can restrict which fields that context can write to.
It is recommended to use different ViewModels for different tasks. If you want to show the user some fields of the Model to edit, then you can do so using EditModel and while saving use CreateModel to create and populate the database. This way you can avoid your database structure to be known to the user, thus ensuring protection and security.

LINQ to entities (Create a new table, display all tables from a database)

I've been trying to figure this out for a few hours now, and no amount og Google search
has yielded anything usefull, I'm currently studying up on my exam and I believe i will need to know these things, so i hope someone can help me!
I have two questions;
Is it not possible to create a new table? (Or Entity i believe it is called)
And also, is it possible to display all table's you have?
I'm making a small forms project where I'm basically just making methods that will carry out the different commands that Linq to Entity proves.
Such as a a controle to create a new table with the name put in the textbox,
a listbox displaying all tables in the database and so on.
Hopefully someone will be able to help.
~Etarnalazure.
1: no, list MOST ORM's it is a data access technology. EntityFramework has schema updates etc., but again this is not "runtime".
2: no, again, this is not a technology to manage databases. YOu only can query what you have classes for.
Well, 2 IS possible, but you have to generate the classes in a separate appdomain at runtime, then use that to query for data.

Need direction in creating an application which generates a crud form

I was wondering if anyone knew of any way i can implement an application which will do the following.
Allow a user to specifiy a connection string to a sql db
Allow a user to specify a table in the db
Allow a user to specify columns from the specified table
Generate Views, a Controller with Crud methods, & Data access code on the fly for the specified table columns in a subdirectory on the current web app.
I'm aware that there are apps that currently do this (such as sharepoints list creation stuff), but i'd like to see how this was accomplished and recreate it for my own learning purposes.
Thanks alot for any help
Take a look at Microsoft's take on scaffolding, also, some time ago I was developing a taxonomy app and found this meta data model in codeproject
Edit: another cool SO link
Have you check out SharpArchitecture?
Anyway I fiddle with MVC 2 based AutoCrud when I'm not saving the world from aliens so I can give some pointers and point to things to check out:
Become familiar with how MVC 2 can auto scaffold up your edit screens
Understand that you'll have to pass "meta" information about your models somehow. In MVC 2, this is called ModelMetadata.
Tackle how to display related or associated models in aggregate root or parent screens
Learn how to generate code, and inspect ddl schema or meta information with T4 templates.
Thats all I can think off for now. This is not an easy task and a comprehensive answer is probably enough to fill a book.

Categories

Resources