15 Mar
2012

Using Entity Framework to query Views without a Key

By definition an Entity should have an Identity. An issue that often comes up with EF is querying views which do not have a primary key. No key, no laundry.

My preferred way to get around this is to create a value object (POCO) and use SqlQuery<T> to materialize results from the view. This is just a ‘Read’ object, so there are no concerns about round-tripping, change tracking etc (hence no key). All that is needed is to make sure the property names of the object match the column names of the view. Not all of the columns from the View need to be in the object, just the ones you want.

There is no server side SQL linq support off the view, but IMO any complex query logic should be contained in the view, and simple WHERE filtering isn’t too bad with SqlQuery<T>.

Find me

RSS
Facebook
Twitter
LinkedIn
SOCIALICON
SOCIALICON

Disclaimer

The opinions and content expressed here are my own and not those of my employer.