I have one specific view created in my DB(joins about 5-6 tables with a left join).This view is added to my edmx (entity framework 1.0) . Recently I noticed that one of the column records obtained using the edmx (linq to entities and then ToList()) got duplicated multiple times though in the database view they were different
Column-N (Expected result/ result from DB view)
---------
data1
data2
data3
data4
data5
Column-N(Actual result generated by entity framework)
---------
data1
data1
data1
data1
data1
I fired up my SQL profiler,got the query which was sent by my application to the SQL Server, ran it and it returned me the expected result.
MSDN has a similar post here and here but the moderator has not elaborated on how to solve this problem. My key happens to be a GUID
The root cause you pointed out I think is correct, the problem is on the application side EF mapping, as EF has different object mapping rules with database. when the query results have been returned from database, the EF will do the mapping on application memory according to its own designed logic.
It's important to take these logic into account when you desingn your view query in your database side. I think you should do some adjustment on your view query.
I am not sure whether you have sorted the problem, if not please provide the database structure related to this issue and the view query you have written.
Thanks Binze
Has someone encountered a similar problem before ?
Entity Framework : A Comprehensive CourseViews can be used in a similar way as you can use tables. To use view as an entity, first you will need to add database views to EDM. After adding views to your model then you can work with it the same way as normal entities except for Create, Update, and Delete operations.
EF5 is built into the core of . NET 4.5, whereas EF6 has been shifted out, and is open source. This means that you must add the new EF6 assemblies to all of the relevant projects in the solution, in particular the entry project. This means that you must remove assembly System.
Entity Framework has the following forms of caching built-in: Object caching – the ObjectStateManager built into an ObjectContext instance keeps track in memory of the objects that have been retrieved using that instance. This is also known as first-level cache.
We can utilize views in entity framework database first approach easily considering a model. However, in entity framework code first approach, we need to do tricks. If we create models of views then it will create tables of those views in add-migration and database update command. We can handle views in entity framework in two ways.
There is option to modify query manually and check the results. We can utilize views in entity framework database first approach easily considering a model. However, in entity framework code first approach, we need to do tricks. If we create models of views then it will create tables of those views in add-migration and database update command.
Currently, Entity Framework Core 5 does not support updating the view, while it is possible in SQL Server database. You can specify a view with a key, though. Just remember to remove HasNoKey in configuration and [Keyless] attribute in the entity. Let’s use the code that we just wrote.
The Entity Framework (EF) is an Object Relational Mapping (ORM) tool that allows developers to work with the database by simply writing .NET code. For an introduction to EF take a look at our earlier tip Intro to Entity Framework with SQL Server . EF has built-in support for using existing views.
The problem is in fact with the key. You have to a) have a unique identifier for each row in the view. and b) map that key accordingly in the edmx. Otherwise as your quote states, the mapping logic will see each subsequent row and figure that it can use the same object instance that it returned before
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With