Home Entity Framework database first, Views have more than one EntityKey
Reply: 0

Entity Framework database first, Views have more than one EntityKey

Mohamed Atia
1#
Mohamed Atia Published in 2015-10-20 13:31:22Z

We are using entity framework 5/6 to generate a database first Model, to expose some views I noticed in the generated model, that many columns are defined as Keys (EntityKey = true) which made entity framewrok include order by statements in almost every query to that view.

I don't know the rule of thumb here, but through trial and error and monitoring the generated SQL queries in SQL profiler, I concluded that Entity Framwork adds order by to all the queries using the columns marked as Entity Keys.

  SELECT [ID]
        ,[Name] 
        ,[Phone]
        ,[Type]
 FROM myView
 ORDER BY [ID] ASC, [Name] ASC, [Type] ASC

If I modified the edmx file through dessigner and set EntityKey = false to Name and Type columns the query is:

  SELECT [ID]
        ,[Name] 
        ,[Phone]
        ,[Type]
 FROM myView
 ORDER BY [ID] ASC

The question is, how does Entity Framework decide that [ID],[Name], and [Type] are Entity Keys in the first place?

The Problem is, so many order by may affect performance, isn't that right?, and the [Name] column is of type XML, so SQL server simply throws exception

  The XML data type cannot be compared or sorted, except when using the IS NULL operator.
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.295353 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO