Building an Entity Framework 4.0 model on views: practical tips

Many development teams and database administrators use views to create an abstraction layer on top of a physical data model. In this article I'll show you how to build an Entity Framework 4.0 (EF4) model upon such a set of views. I'll create a couple of views in SQL Server -some of them indexed-, import them in a model using the Visual Studio designer, decorate them with different types of associations (foreign key and mapped table), and finally attack the model with some Linq queries. This is how the model looks like:

Creating the views

Views that are directly consumed by your application should be stable. That's why I prefer such views to be declared with the SCHEMABINDING option:

CREATE VIEW U2UConsult.Person WITH SCHEMABINDING

AS

SELECT BusinessEntityID AS PersonId,

       Title,

       FirstName,

       LastName

  FROM Person.Person

The SCHEMABINDING option protects a view against modifications to the schema of the underlying tables, at least modifications that would invalidate that view. E.g. it becomes impossible to drop a column on which the view relies:

Optional step: indexing the views

Sometimes it makes sense to persist the view on disk and let SQL Server make sure that its content remains in sync with the underlying tables. This is very useful for complex views (with lots of joins and calculations) on stable tables. All we need to do is creating a clustered index on the view:

CREATE UNIQUE CLUSTERED INDEX [IUX_Person]

ON [U2UConsult].[Person]([PersonId])

WITH (FILLFACTOR = 100)

 

Importing the views

You import a view into an entity model just like you import a table. But views -even indexed ones- can not have primary or foreign keys in the database, so there's no metadata to import. The visual designer overcompensates this by inferring a key composed of all non-nullable columns. This is not a good idea: the first thing you need to do is define the primary key of the view:

before after

Now do the same steps for the Address view:

CREATE VIEW U2UConsult.Address WITH SCHEMABINDING

AS

SELECT AddressID AS AddressId,

       AddressLine1,

       AddressLine2,

       PostalCode,

       City

FROM Person.Address

GO

 

CREATE UNIQUE CLUSTERED INDEX [IUX_Address]

ON [U2UConsult].[Address]([AddressId])

WITH (FILLFACTOR = 100)

GO

 

Defining 1-to-1 or 1-to-many relationships

In a table, you would express 1-to-1 or 1-to-many relationships by creating a foreign key relationship. In an entity model, you can do the same with views. For starters, define a new association between two views. The model looks good now, and IntelliSense will help you while building Linq queries against it. However, your missing an important ingredient: the physical model doesn't know to solve the association: 

When creating the association, don't forget to check the 'add foreign key properties' box:

If necessary, you can refine the data store binding by clicking on the ellipsis next to 'referential constraint' in the properties window:

After that, you need to remove the view from the designer. The view is used as entity ànd as association, EF4 does not like that:

Defining many-to-many relationships

Many-to-many relations are generally implemented through an intermediate table. A many-to-many relationship between two views is built exactly the same way. The AdventureWorks2008 database has an intermediate table between Address and BusinesEntity (= Person): BusinessEntityAddress. Unfortunately we can't use this table to carry the association. Strangely enough the entity framework requires that all its physical (SSDL) primary key fields should be mapped. Using that table as the glue between Persons and Addresses yields the following error:

 

As a work around, you could define the link table as a view:

CREATE VIEW U2UConsult.PersonAddress WITH SCHEMABINDING

AS

SELECT DISTINCT BusinessEntityID AS PersonId,

       AddressID AS AddressId

FROM Person.BusinessEntityAddress

Then add it to the entity model, and map the association to it. The foreign key checkbox will be disabled for many-to-many associations:

 

Both types of associations were created using the designer. I didn't need to manually tweak the SSDL-part of the model. So when we upgrade it from the database they will remain intact.

Querying the views

For Linq it doesn't matter where the data comes from, so you use the views like you would use a table:

Person person = (from p in entities.Person.Include("Phones"

                 where p.PersonId == 1

                 select p).FirstOrDefault();

This gives the following result:

If EF4 performance matters to you, you might want to (re-)read this article.

Source

Here's the sample project, all SQL queries are included: U2UConsult.EF4.Views.Sample.zip (14,33 kb)

Enjoy!


Comments (3) -

August 24. 2011 11:35 PM

kizedek

Could you elaborate on the following step?

"After that, you need to remove the view from the designer. The view is used as entity ànd as association, EF4 does not like that:"

If you remove the view from the designer, then, well they are removed and you cant use them?

kizedek

October 15. 2011 05:28 AM

Jeremy Holovacs

Indexing the view tends to be a lot more problematic than you seem to suggest.  There are very few cases I have run into where SQL Server will actually let you index a view; the instant you have subqueries, CTE's, most aggregate functions, or a number of other common constructs in your view (and most of them do, they are meant to bury that kind of complex logic) placing a clustered index on the view becomes impossible.

Jeremy Holovacs

November 26. 2011 10:25 AM

Voss

I also have the same question as the post above:

Could you elaborate on the following step?

"After that, you need to remove the view from the designer. The view is used as entity ànd as association, EF4 does not like that:"

If you remove the view from the designer, then, well they are removed and you cant use them?


Voss

May 16. 2012 09:29 PM

sac louis vuitton

le virtuels et aux simulations de tir.(lien en anglais) pour supprimer le programme de son ordinateur.?? [url=http://www.lvpascher1.com/]sac louis vuitton[/url] "L'objectif est ainsi de prendre des parts de marché à la voiture, par rapport à laquelle l'autocar représente un moyen de transport nettement plus écologique et très sécurisé".Le candidat s'est également réjoui d'avoir irrité la gauche et les syndicats en annon? [url=http://www.lvpascher1.com/]sac vuitton[/url] Celle-ci promet en effet une machine "immunisée contre les virus". [url=http://www.lvpascher1.com/]sacs louis vuitton[/url]ler l'immigration, améliorer la sécurité, changer l'Europe pour qu'elle ne soit plus une "passoire",? Je parle aussi bien à ceux qui ont voté Fran? [url=http://www.lvpascher1.com/]louis vuitton site officiel[/url]: se faire passer pour un no life?Stora :? [url=http://www.lvpascher1.com]louis vuitton pas cher[/url]Selon Dr?

sac louis vuitton

May 16. 2012 09:29 PM

sac louis vuitton

le virtuels et aux simulations de tir.(lien en anglais) pour supprimer le programme de son ordinateur.?? [url=http://www.lvpascher1.com/]sac louis vuitton[/url] "L'objectif est ainsi de prendre des parts de marché à la voiture, par rapport à laquelle l'autocar représente un moyen de transport nettement plus écologique et très sécurisé".Le candidat s'est également réjoui d'avoir irrité la gauche et les syndicats en annon? [url=http://www.lvpascher1.com/]sac vuitton[/url] Celle-ci promet en effet une machine "immunisée contre les virus". [url=http://www.lvpascher1.com/]sacs louis vuitton[/url]ler l'immigration, améliorer la sécurité, changer l'Europe pour qu'elle ne soit plus une "passoire",? Je parle aussi bien à ceux qui ont voté Fran? [url=http://www.lvpascher1.com/]louis vuitton site officiel[/url]: se faire passer pour un no life?Stora :? [url=http://www.lvpascher1.com]louis vuitton pas cher[/url]Selon Dr?

sac louis vuitton

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Download the U2U brochure

Download Brochure

Receive the U2U Newsletter. Submit your email address:
 
 


 


Search

rss  RSS

Tags

Archive

Blogroll