My EF schema has a mixture of tables and views from my database. My view entities are all read-only, I want to be able to update/insert into these entities. I've tried the following post without any luck:
http://smehrozalam.wordpress.com/2009/08/12/entity-framework-creating-a-model-using-views-instead-of-tables/
Anyone have another ideas/pointers - this must be doable.
cheers
David
Generally the post is correct. Using views in EF is hard. Another trick is first using tables to define your model and then replace tables in database with views with same names as tables.
Have you tried Instead of Triggers?
http://www.codeproject.com/Articles/236425/How-to-insert-data-using-SQL-Views-created-using-m
Related
Just getting started with EF v6 and trying to see if I can retrofit it to an existing database. In some ways it looks like the existing database will map well to an object model (HealthProviders, Patients, Visits) however in other ways some of the tables won't map easily to objects. That being said does EF require mapping all database objects to code or can you pick and choose which database objects are modeled in EF?
Yes, you can. Using database first - the default - you get to choose which objects - tables, views, stored procedures - you want to map.
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.
What should I do if I have a Model which properties have to be filled from multiple tables from the EDMX file?
Should the Controller or specific actions do this work for me? Or is EF the wrong technology in this case because I [maybe] have to execute multiple statements to built a single model object for my view?
Update: Why is nobody mentioning ViewModels? I think this is the way to go? http://jabbr.net/#/rooms/aspnetmvc - One of the guys told me that it´s a standard approach to build a model from multiple entities from the db. So I´ll accept that?! Can anyone please give me a last "OK" on that? I think all this stuff has confused me because my mind wasn´t clear about the term "Model". I doesn´t have to be a 1:1 relationship and what I´m actually working with are VIEWmodels... right?
Code First approach. This way you control how the db maps to your model.
I ended up creating ViewModel classes.
I am learning code first and I have project to be used with an existing database.
I am a bit confused of what I meant to be doing.I will explain:
Do I need to create an entityconfiguration for each table in my existing database?
Within this EntityConfiguration for each table do I need to create foreign key relationships?
Do I need to do a ToTable for each table in my existing database?
Is there any free tool "codeplex" that pointing to an existing db will generate this codeFirst stuff?
I have seem few blogs about "EF Code first with existing db" but I am not sure or was not clear to me If Need to create this stuff or I will be getting strange errors like "MyColumn_MyColum" basically as if codeFirst is trying to build some FKs or something.
can somebody clarify?
thanks a lot. I know there are few questions but if you can answer 01 or 2 that would be fine.
thanks again
If you want the code to be generated for you use database-first approach with DbContext API. Simply create EDMX file from your database and let DbContext Generator template generate all entities and context for you.
DbContext Fluent API is mainly targeted to the code-first development approach where EF will created database for you from the code you provided. It can be used with existing database but it requires much more skills and understanding of mapping wich EF can provide to you.
Generally:
You don't need to provide EntityConfiguration for each table if you follow some naming conventions (entity name is singular form of table name, all properties have the same name, primary key in table and entity is named as Id or EntityNameId, etc.).
You don't need to define relationships manually if you follow conventions with exposing navigation properties and possibly also foreign key properties. The issue can be naming of many-to-many keys and junction tables.
ToTable is needed only if your entity does not follow naming convention or if you map some advance inheritance or splitting.
EF uses a lot of default conventions which drive how the names should be defined. Conventions can be removed.
You will not do anything wrong if you define EntityConfiguration for each table - it will at least allow you learning what is needed and your mapping will be explicit / self documented.
I setup the builder and used CreateDatabase to generate the database.
However the table columns created are ordered alphabetically. Is there a way to control the order?
As far as I know, code only doesn't have support to such feature.
Edit: The new CTP5 has an attribute called ColumnAttribute with some features like Order.
Hope it helps.