![]() |
|
|
By: Ray Barley | Read Comments (2) | Print Ray is a Principal Architect at RDA Corporation and a MSSQLTips.com BI Expert. Related Tips: More |
|
I'm a DBA and the developers in my organization want to use the Entity Framework (EF) in their applications. While I understand how EF can dynamically generate the SQL for queries, we have many existing views that I would like to be able to continue to use. How can I do this with the Entity Framework?
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. Essentially it will treat the results returned from a view as an entity, similar to a table. When you use Visual Studio's Entity Data Model Wizard to generate a model from an existing database, an entity is created for each table that you select. When you select a view an entity will be created for it as well.
To demonstrate let's start out with a very simple hypothetical schema that represents a database for the the MSSQLTips web site:
As is typical we have a normalized schema with foreign key relationships between the Tip table and the TipCategory and Author tables. In order to simplify querying we make use of the following view to return the data that we need for a particular web page:
CREATE VIEW [dbo].[vTipList] AS SELECT t.TipID , a.FirstName + ' ' + a.LastName Author , t.Title , c.TipCategoryName , t.PublishDate , t.URL FROM dbo.Author a INNER JOIN dbo.Tip t ON t.AuthorID = a.AuthorID INNER JOIN dbo.TipCategory c ON c.TipCategoryID = t.TipCategoryID WHERE (t.PublishDate IS NOT NULL)
Creating the EF Model
We are now ready to create our EF model that will make our view available. My starting point is a Visual Studio 2008 SP1 Console application. Add an ADO.NET Entity Data Model to the project and name it MSSQLTipsModel.edmx as shown below:
Click Add then choose Generate from database in the Choose Model Contents dialog as shown below:
Click Next then select an existing database connection or click New Connection to create one as shown below:
Note that the Save entity connection settings in the App.Config checkbox is checked which puts the necessary connection information into the application's configuration file. Typically you would want to do this. Click Next then select the views to be included in the model from the Choose Your Database Objects dialog as shown below:
Click Finish to complete the Entity Data Model Wizard. You will see the following message displayed:
The table/view 'EFTipsSample.dbo.vTipList' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.
When you create a model, EF will automatically generate the classes required to query and manipulate the entities. However, this is not the case for an entity that is based on a view. You'll get what you need to query the entity but you can't perform inserts, updates or deletes automatically. You can create your own stored procedures to do this. You can refer to our earlier tip How To Use SQL Server DML Stored Procedures with the Entity Framework for the details.
Now we are ready to write some sample code to query based on the view in our EF model. You can take a look at the code generated by EF; you will find it in the MSSQLTipsModel.Designer.cs file. There are a couple of noteworthy things to point out:
The following is the simplest bit of code that will perform a query using the view and display each row and its columns:
using (EFTipsSampleEntities ctx = new EFTipsSampleEntities())
{
var result = from t in ctx.vTipList
select t;
foreach (vTipList t in result)
{
Console.WriteLine("{0} {1} {2} {3} {4}",
t.TipID, t.Author, t.TipCategoryName,
t.Title, t.URL);
}
}
The above code snippet uses LINQ to Entities in order to perform the query (shown in bold). The output looks like this:
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
| Wednesday, February 01, 2012 - 10:27:42 AM - Colby | Read The Tip |
|
I am also a DBA and our group is also using Entity Framework. This article is very interesting and learned a lot. There are a lot of "how to" using SQL and the entity framwork that you could write about. The question is in the entity framework model who owns the SSDL, the MSL and CSDL. I think the DBA owns the SSDL. Thoughts? |
|
| Wednesday, February 01, 2012 - 10:40:22 AM - Ray Barley | Read The Tip |
|
I think the DBA should own everything! Seriously though the DBA should own the SSDL and the MSL. The beauty of EF is that really all a developer should care about is the CSDL and most would probably be happy with that I think. I work both as a developer and a DBA and when I'm building an app it would be great if I had the CSDL and everything else is just a black box.
|
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |