I currently have an EF class that is backed by a database view (joining multiple tables). To make it updatable, I need to change the class to be backed by a database table. I am using Entity Framework 4.1 Code First and can't figure out how to set up those relationships?
To simplify, I currently have a Categories class that returns the Category Name (Categories table) and Category Type Name (CategoryTypes table). These are both in the database view that I currently use. I want to change to a ViewModel that brings back both of these fields directly from their tables and joins properly, that way when a user updates a Category Name, EF should be able to properly handle the update (since it will be updating the table instead of the view). Any tips on how to do this?
Table is a table - it is a single database object. If you want to remove your view and replace it with a table you need to delete your current tables (Categories and CategoryTypes) and create a single table which will contain denormalized data. That is pretty bad solution and it will cause you problems in the whole application.
Just to simplify description: It is not possible to replace your view constructed by joins among several tables with a table and it is not possible to make your view updatable.
You are doing it wrong because you are obviously mapping view models directly to your database. Map Catagories and CategoryTypes to entities load Category with its CategoryType and flatten them to your view model in your application logic (or load the view model through projection). Once user updates your view model decompose it back to separate entities and save them.
Related
What I hope is a basic question,
I am designing an MVC project with the entity framework and code first and in it has a number of normalized tables that later will make up a combined view.
For example say I have a table called JOBS. This table has foreign keys for a CUSTOMER table, a STATUS table, a JOBSTYPE table.
If I want a view (a page) that displays the job with the customer, its status and its jobtype how do I manage this outcome?
In other words if I want a page that shows the job, the customer and the jobs address (sourced from the address table - itself linked via a foreign key in the customer table) how do I do the view for this?
Further, with a focus on CRUD, If I want an update page how do I display a page that has text boxes to update things like the job's address or the status which are in different tables to the actual job table.... and to press a button on the page saying "update" and each table updating automatically..
Look forward to any help clearing the confusion...
Kind Regards
Simon
Just as the question, this answer is hypothetical as well. What you can do is have a look at your page design and figure out the columns/properties you want to show from multiple tables, you then create a ViewModel for this page, then you can write a LINQ projection query to bring the results as your viewmodel.
Another other option will be to use lazy loading all linked tables and render related entities, but this this approach you have to make sure that the EF context is not disposed till the whole view has rendered.
The ViewModel and Projection approach also works well with updates and your update action will take in your view model and translate back to EF entities for updating.
For translations from ViewModel to EF Model entities and vice versa you can use automapper
We have a table (AttendanceType) in our database which have many fields with multiple options. this multiple options are defined in single table. So, instead of creating separate Option table for each option we have single table (Option_Data) with key to identify each option type (Record).
Example : AttendanceType table has following fields
ID
Description
Category (Payroll / Accrual)
Type (Hours / Days)
Mode (Work hours / Overtime / ExtraHours)
Operation (Add / Minus)
These fields have options (data as shown above in brackets) which comes from Option_data table. We have created separate views from this Option_data table example: vwOption_Attendance_Mode, vwOption_Attendance_Operation etc.
Now, how we can link this view in breeze so the reference data come automatically.
We are using EF6, SqlServer, Asp.Net WebApi. When the table relationship is defined in SQL Server Breeze works perfectly and manages the relational data. In this case we cannot define relationship in SQL Server between Table and Views.
How we can code it so Breeze can manage the relational / reference data for us? If you require further clarification please let me know.
Edit # Jay Traband : Let say a single table (ie: AttendanceType) has fields which get reference/lookup data for its field from Views. How in breeze we can relate them (table with views), as in SQL Server we cannot.
My reference points is when Tables are related breeze does excellent job. I want to achieve same with table and views.
Breeze gets its metadata (including the relationships between entities) from EF. You'll need to tell EF about the relationship between the tables and views, even if there is no such relationship defined in SQL Server. This SO post provides some clues, and this blog post gives some related information about creating relationships in the designer.
I am developing MVC application and using razor syntax. I have used model first method.
I have two entities, Customer and Lead. Lead is inherited from Customer.
When IsActive property is true then Customer treated as a Lead, otherwise it will be a customer.
Please check edmx file image.
Now, In regular entities we just deal with single entity and single table.
In this case how can I handle , Save and Load process. beacuse I have to store and load the record from 2 tables of DB.
Is Regular Index View will work here ?
When using inheritance in the Entity Framework you will have a single DbSet on your DbContext that exposes your hierarchy. In your database you have several options for configuring your table structure. For example you can use:
Table per Hierarchy
Table per Type
Table per Concrete type
(See this blog for a nice explanation: Inheritance in the Entity Framework.
In your queries however, you don't have to think about this. Your queries will have the following structure:
var leads = from l in dbcontext.Leads.OfType<Customer>()
select l;
The OfType() filters your collection to a subtype in your hierarchy. If you skip the OfType you will get both customers and leads in your resulting query.
I have a few problems but I'm going to start with the first and smallest. I have two models with a many-many relationship, Category and Project. In each model I have an ICollection of the other model. I initialize the collection as a new HashSet() for the Project constructor and vice-versa for the Category. I've read online that this will create a new table in your database with the PK of each model as the PK in the new table. I custom named them and whatnot through Fluent API but you get the idea.
This worked out great. So I make my Controllers and create and use scaffolding to create the CRUD views. I create a few categories.. Great. Now when I get to creating a a new Project, what I want is it to show me a list of the Categories I previously created, and to require at least one be selected before pushing through the Project. The view shows no categories to select at all and allows it to go through as null. I know how to make a property required but I don't know how to make a collection property required and grab all of the categories from the database to present in the Project create view for selecting....
Try :
Context.Project.Include(p=>p.Category)
inside your query code,
you will need
using System.Data.Entity;
to get the include method
For enabling lazy load, you will also need to mark your properties as "virtual"; otherwise, you will always have to eager load them using the .Include() method.
I'm not sure if there is a difference in these two methods. If so, which would be considered the better practice when submitting more than one object to the View.
Having the controller make separate calls to the datalayer for each object model and then wrapping the objects in a model to send to view.
Defining a "presentation" model and having the controller call that single model to send to the view.
other...?
I'm assuming here that you have a view that presents some information from more than one model, perhaps in a list format. For example, you may have a model of a customer which has a set of contacts, but in your list you want to choose to show some of the customer details along with the name and phone number of the primary contact. What I would typically do in a situation like this is define a specific "presentation" model that consists of just those details that I may want to show in this combined view. It would typically be a read-only model. Using LINQ to SQL I might even define this as a table-valued function (to support search) and associate it with a view that encapsulates the join of the various tables. With both you can add the view-based "presentation" model to your DBML and associate the table-valued function with it as a method on the data context.
I prefer doing this because I believe that it is more efficient in terms of queries to construct the query on the server and simply use it from the code. If you weren't using the table-valued function for searching, you might be able to construct the query in code and select into a "presentation" class. I would favor an actual class over an anonymous type for ease of use in the view. Getting the properties from an anonymous type in the view would be difficult.
You should send to the View a single object, sometimes termed a ViewModel object, containing all the data (including domain model objects) that the view will need.