Mike Borozdin's Blog

A blog about programming, web and IT in general

Put EntityDataSource Attributes to Code-Behind

The EntityDataSource control is a very powerful one. It allows you to rapidly create database driven application. You don’t have to manually write code for extracting, modification and deleting records from the database. Moreover since this code is backed by Entity Framework, you are not tied up with a particular database schema and can easily change it or even choose other database application.

However when working with EntityDataSource sometimes I feel like I using plain old SqlDataSource and simply have too much unnecessary code in my .aspx files. For instance, you may end up have code like this one:

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=NorthwindEntities" 
    DefaultContainerName="NorthwindEntities" EnableDelete="True" 
    EnableInsert="True" EnableUpdate="True" EntitySetName="Products" Include="Categories, Suppliers" AutoGenerateWhereClause="true">
    <WhereParameters>
        <asp:QueryStringParameter Type="Int32" Name="CategoryID" QueryStringField="CategoryID" />
        <asp:QueryStringParameter Type="Int32" Name="SupplierID" QueryStringField="SupplierID" />
    </WhereParameters>
</asp:EntityDataSource>

That certainly isn’t cool at all. Because it simply shouldn’t be in an .aspx file, not only because it breaks application layers, but simply because it inconvenient. It clutters .aspx files which should be templates only. What if a designer meets code like this? What if accidently change something?

Instead it’s reasonable to move all these lines of code to code-behind.

So, in your .aspx file you just leave this declaration:

<asp:EntityDataSource ID="dsProducts" runat="server" />

 

While put all the attribute assignments to code-behind:

NorthwindEntities db = new NorthwindEntities();

dsProducts.ConnectionString = db.Connection.ConnectionString;
dsProducts.DefaultContainerName = "NorthwindEntities";
dsProducts.EntitySetName = "Products";
dsProducts.Include = "Categories, Suppliers";

dsProducts.EnableUpdate = true;
dsProducts.EnableInsert = true;
dsProducts.EnableDelete = true;
dsProducts.AutoGenerateWhereClause = true;

dsProducts.WhereParameters.Add(new QueryStringParameter("CategoryID", TypeCode.Int32, "CategoryID"));
dsProducts.WhereParameters.Add(new QueryStringParameter("SupplierD", TypeCode.Int32, "SupplierID"));

 

In fact, the same technique applies to any other data source control, like LinqDataSource or even ObjectDataSource.


Posted by Mike Borozdin on Monday, March 23, 2009 8:43 AM GMT
Shout it Kick it!  
Permalink | Comments (6) | Post RSSRSS comment feed

Free Entity Framework Learning Guide

I came across a wonderful learning guide for the Entity Framework that explains many essential and complex things about the Entity Framework, including:

  • Modeling Entities
  • Lazy Loading
  • Inheritance
  • Working with objects
  • Performance
  • Working with Stored Procedures

Moreover this 500 pages long learning guide is absolutely free and can be downloaded here with all examples.

http://weblogs.asp.net/zeeshanhirani/archive/2008/12/18/my-christmas-present-to-the-entity-framework-community.aspx


Posted by Mike Borozdin on Monday, January 19, 2009 2:02 AM GMT
Shout it Kick it!  
Permalink | Comments (2) | Post RSSRSS comment feed

Understanding Attaching/Detaching Objects in LINQ to SQL and in the Entity Framework

LINQ to SQL and the Entity Framework are very powerful tools, however as in many other useful tools there are some things you should be aware of when working with them. One of those things is object attaching and detaching. Let’s have a concrete example.

As you already know, you can retrieve an object from the database, update or remove it and all the necessary changes will be submitted to the database.

But what if you don’t want to perform additional SELECT query? Instead, you want just to perform one necessary query, either to update an object or remove it

Well, a quite logical idea is to create an object, set an appropriate ID attribute and then update/delete it:

NorthwindDataContext db = new NorthwindDataContext();
 
Product product = new Product { ProductID = 1 };
 
db.Products.DeleteOnSubmit(product);
 
db.SubmitChanges();

Okay, but it won’t work. The code will gets compiled, but when executed it will throw an exception stating: “Cannot remove an entity that has not been attached.” Well, in fact, it’s quite obvious, because the object context is simply unaware of that object, so let it know about the object. We just need to attach the object to the object context:

NorthwindDataContext db = new NorthwindDataContext();
 
Product product = new Product { ProductID = 1 };
db.Products.Attach(product); //that necessary line
 
db.Products.DeleteOnSubmit(product);
 
db.SubmitChanges();

So, if you compile it now, you will get no exceptions and the necessary product will be removed from the database. You can use the same technique when you need to update an object without having to retrieve it from the database and the same thing applies to the Entity Framework, although the code is slightly different:

NorthwindEntities db = new NorthwindEntities();

Product product = new Product { ProductID = 10 };
product.EntityKey = new EntityKey("NorthwindEntities.Products", "ProductID", 10);
db.Attach(product);

db.DeleteObject(product);

db.SaveChanges();

Posted by Mike Borozdin on Thursday, January 15, 2009 4:12 AM GMT
Shout it Kick it!  
Permalink | Comments (3) | Post RSSRSS comment feed

The List of the LINQ to SQL and Entity Framework Providers

If you are using Microsoft SQL Server you don’t experience any problems, SQL Server are supported by both LINQ to SQL and the Entity Framework. Frankly speaking, it couldn’t be otherwise, since they all are made by Microsoft.

However, if you are using a non-Microsoft database engine, it’s certainly worth knowing if you can use LINQ to SQL or the Entity Framework with it. Moreover, it’s always better if there is native support.

  LINQ to SQL (native) LINQ to SQL (3rd party) Entity Framework (native) Entity Framework
(3rd party)
SQL Server Yes who cares? Yes who cares?
SQl Server CE Yes who cares? Yes who cares
Oracle No DBLinq
LINQ to Oracle
LightSpeed
dotConnect
Yes dotConnect
EFOracle
DB2 No ? Yes ?
MySQL No DBLinq
LightSpeed
Planned dotConnect
PostgreSQL No Npgsql
DBLinq

LightSpeed
dotConnect
No Npgsql
dotConnect

According to the table, the Entity Framework is natively supported by a greater number of databases than plain LINQ to SQL. Anyway, if you cannot find a native provider, you can always find a 3rd party one, but you must remember that some of them are not free and/or may lack some features.

Feel free to correct this table, if it contains a mistake and comment on individual providers. It’s reasonable to keep this list comprehensive. Although I wish every major database would have native support of both technologies.


Posted by Mike Borozdin on Tuesday, January 06, 2009 1:10 PM GMT
Shout it Kick it!  
Permalink | Comments (7) | Post RSSRSS comment feed

Creating Entity Framework Driven ASP.NET Application

Introduction

I have already written several posts on the Entity Framework where I described the power of this particular ORM tool. I also mentioned the book on the Entity Framework written by a Microsoft MVP - Joydip Kanjila. This time I will publish an extract from the book that shows how you can build ASP.NET application by using the Entity Framework and the EntityDataSource control. It can also give a glimpse of the Entity Framework is, if you have no experience in it.

The tutorial covers the following topics:

  • Creating the Entity Data Model by using a graphical utility built-in Visual Studio 2008 SP1
  • Creating the Entity Data Model by using a command line utility
  • Using the EntityDataSource ASP.NET control
  • Displaying the data in a GridView

This tutorial uses a particular database, but in fact you can use any database you already have.

Creating an Entity Data Model.

You can create the ADO.NET Entity Data Model in one of the two ways:

  • Use the ADO.NET Entity Data Model Designer
  • Use the command line Entity Data Model Designer called EdmGen.exe

We will first take a look at how we can design an Entity Data Model using the ADO.NET Entity Data Model Designer which is a Visual Studio wizard that is enabled after you install ADO.NET Entity Framework and its tools. It provides a graphical interface that you can use to generate an Entity Data Model.

Creating the Payroll Entity Data Model using the ADO.NET Entity Data Model Designer

Here are the tables of the 'Payroll' database that we will use to generate the data model:

  • Employee
  • Designation
  • Department
  • Salary
  • ProvidentFund

To create an entity data model using the ADO.NET Entity Data Model Designer, follow these simple steps:

l>
  • Open Visual Studio.NET and create a solution for a new web application project as seen below and save with a name.

  • Switch to the Solution Explorer, right click and click on Add New Item as seen in the following screenshot:

  • Next, select ADO.NET Entity Data Model from the list of the templates displayed as shown in the following screenshot:

            
  • Name the Entity Data Model PayrollModel and click on Add.
  • Select Generate from database from the Entity Data Model Wizard as shown in the following screenshot:

    Note that you can also use the Empty model template to create the Entity Data Model yourself.

    If you select the Empty Data Model template and click on next, the following screen appears:

    As you can see from the above figure, you can use this template to create the Entity Data Model yourself. You can create the Entity Types and their relationships manually by dragging items from the toolbox. We will not use this template in our discussion here. So, let's get to the next step.

  • Click on Next in the Entity Data Model Wizard window shown earlier.
  • The modal dialog box will now appear and prompts you to choose your connection as shown in the following figure:

  • Click on New Connection Now you will need to specify the connection properties and parameters as shown in the following figure:

    We will use a dot to specify the database server name. This implies that we will be using the database server of the localhost, which is the current system in use.

  • After you specify the necessary user name, password, and the server name, you can test your connection using the Test Connection button. When you do so, the message Test connection succeeded gets displayed in the message box as shown in the previous figure.
  • When you click on OK on the Test connection dialog box, the following screen appears:

    Note the Entity Connection String generated automatically. This connection string will be saved in the ConnectionStrings section of your application's web.config file. This is how it will look like:

      <connectionStrings> 
      <add name="PayrollEntities" connectionString="metadata=res:// *;
      provider=System.Data.SqlClient;provider connection string=&quot;
      Data Source=.;Initial Catalog=Payroll;User ID=sa;Password=joydip1@3;
      MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
  • When you click on Next in the previous figure, the following screen appears:

  • Expand the Tables node and specify the database objects that you require in the Entity Data Model to be generated as shown in the following figure:

  • Click on Finish to generate the Entity Data Model.
  • Here is the output displayed in the Output Window while the Entity Data Model is being generated:

    Your Entity Data Model has been generated and saved in a file named PayrollModel.edmx. We are done creating our first Entity Data Model using the ADO.NET Entity Data Model Designer tool.

    When you open the Payroll Entity Data Model that we just created in the designer view, it will appear as shown in the following figure:

    Note how the Entity Types in the above model are related to one another. These relationships have been generated automatically by the Entity Data Model Designer based on the relationships between the tables of the Payroll database.

    In the next section, we will learn how we can create an Entity Data Model using the EdmGen.exe command line tool.

    Creating the Payroll Data Model Using the EdmGen Tool

    We will now take a look at how to create a data model using the Entity Data Model generation tool called EdmGen.

    The EdmGen.exe command line tool can be used to do one or more of the following:

    • Generate the .cdsl, .msl, and .ssdl files as part of the Entity Data Model
    • Generate object classes from a .csdl file
    • Validate an Entity Data Model

    The EdmGen.exe command line tool generates the Entity Data Model as a set of three files: .csdl, .msl, and .ssdl. If you have used the ADO.NET Entity Data Model Designer to generate your Entity Data Model, the .edmx file generated will contain the CSDL, MSL, and the SSDL sections. You will have a single .edmx file that bundles all of these sections into it. On the other hand, if you use the EdmGen.exe tool to generate the Entity Data Model, you would find three distinctly separate files with .csdl, .msl or .ssdl extensions.

    Here is a list of the major options of the EdmGen.exe command line tool:

    Option

    Description

    /help

    Use this option to display help on all the possible options of this tool. The short form is /?

    /language:CSharp

    Use this option to generate code using C# language

    /language:VB

    Use this option to generate code using VB language

    /provider:<string>

    Use this option to specify the name of the ADO.NET data provider that you would like to use.

    /connectionstring:

    <connection string>

    Use this option to specify the connection string to be used to connect to the database

    /namespace:<string>

    Use this option to specify the name of the namespace

    /mode:FullGeneration

    Use this option to generate your CSDL, MSL, and SSDL objects from the database schema

    /mode:EntityClassGeneration

    Use this option to generate your entity classes from a given CSDL file

    /mode:FromSsdlGeneration

    Use this option to generate MSL, CSDL, and Entity Classes from a given SSDL file

    /mode:ValidateArtifacts

    Use this option to validate the CSDL, SSDL, and MSL files

    /mode:ViewGeneration

    Use this option to generate mapping views from the CSDL, SSDL, and MSL files




    Entity Framework Tutorial
    Entity Framework Tutorial Learn to build a better data access layer with the ADO.NET Entity Framework and ADO.NET Data Services
    • Clear and concise guide to the ADO.NET Entity Framework with plentiful code examples
    • Create Entity Data Models from your database and use them in your applications
    • Learn about the Entity Client data provider and create statements in Entity SQL
    • Learn about ADO.NET Data Services and how they work with the Entity Framework

    http://www.PacktPub.com/entity-framework-tutorial/book



    Note that you basically need to pass the connection string, specify the mode, and also the project name of the artifact files (.csdl, .msl, and the .ssdl files) to be created. To create the Entity Data Model for our database, open a command window and type in the following:

    edmgen /mode:fullgeneration /c:"Data Source=.;Initial Catalog=Payroll;User ID=sa;
    Password=joydip1@3;" /p:Payroll

    This will create a full ADO.NET Entity Data Model for our database. The output is shown in the following figure:

    You can now see the list of the files that have been generated:

    You can validate the Payroll Entity Data Model that was just created, using the ValidateArtifacts option of the EdmGen command line tool as shown below:

    EdmGen /mode:ValidateArtifacts /inssdl:Payroll.ssdl /inmsl:Payroll.msl /incsdl:Payroll.csdl

    When you execute the above command, the output will be similar to what is shown in the following figure:

    As you can see in the previous figure, there are no warnings or errors displayed. So, our Entity Data Model is perfect.

    The section that follows discusses the new Entity Data Source control which was introduced as part of the Visual Studio.NET 2008 SP1 release.

    The ADO.NET Entity Data Source Control

    Data controls are those that can be bound to data from external data sources. These data sources may include databases, XML files, or even flat files. ASP.NET 2.0 introduced some data source controls with a powerful data binding technique so the need for writing lengthy code for binding data to data controls has been eliminated.

    In ASP.NET, the term Data Binding implies binding the controls to data retrieved from a data source and providing a read or write connectivity between these controls and the data that they are bound to.

    The Entity Data Source control is an example of a data control that is included as part of the Visual Studio 2008 SP1 release and can be used to bind data retrieved from an Entity Data Model to the data bound controls of ASP.NET. If you have installed Visual Studio 2008 SP1, you can see the EntityDataSource control listed in the Data section of your toolbox.

    If you cannot locate the EntityDataSource control in the toolbox, follow these steps:

    1. Right-click on the Toolbox and select the Choose Items option as shown in the following figure:

              
    2. From the list of the components displayed, scroll down to locate the EntityDataSource in the .NET Framework Components tab. Refer to the following figure:

    3. Now, check the checkbox next to the EntityDataSource component and click on OK.

    The ADO.NET Entity Data Source control is now added to your toolbox as shown in the following figure:

    If the EntityDataSource component is not listed in the list of the componentsdisplayed in the Choose Toolbox Items window, you will have to add it manually. To do this, click on the Browse button in the Choose Toolbox Items window, locate theSystem.Web.Entity.dll in the folder in your system where Microsoft .NET Framework 3.5 has been installed and click on OK.

    Implementing Our First Application Using the Entity Framework

    In this section, we will learn how to use the Entity Data Model and the Entity Data Source Control to implement our first program using the Entity Framework. We will use a GridView control to display bound data.

    Refer to the solution we created earlier using the Entity Data Model Designer. Now, follow these steps:

    1. Drag and drop an Entity Data Source control from the toolbox onto your  Default.aspx web form.
    2. Now, click on the Configure Data Source option to specify the data source. Refer to the following figure:

    3. Specify the Connection String and DefaultContainerName and then click on  Next.
    4. Specify the fields you would want to retrieve from the database table and click on Finish when done.
    5. Now, drag and drop a GridView control from the toolbox onto the  Default.aspx web form as seen in the following figure:

    6. Next, use the Choose Data Source option of the GridView control to associate its data source with the Entity Data Source control we created earlier. Refer to the following figure:

    Here is how the markup code of the GridView control looks with its templates defined. Note how the DataSourceID of the GridView control has been associated with the Entity Data Source control we created earlier.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID" 
    DataSourceID="SqlDataSource1" BorderColor="Black" BorderStyle="Solid" Width="400px">
    <Columns>
    <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="True"
    SortExpression="EmployeeID" />
    <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
    <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
    <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
    </Columns>
    </asp:GridView>

    We are done! When you execute the application, your output should be similar to what is shown in the following figure:




    Entity Framework Tutorial
    Entity Framework Tutorial Learn to build a better data access layer with the ADO.NET Entity Framework and ADO.NET Data Services
    • Clear and concise guide to the ADO.NET Entity Framework with plentiful code examples
    • Create Entity Data Models from your database and use them in your applications
    • Learn about the Entity Client data provider and create statements in Entity SQL
    • Learn about ADO.NET Data Services and how they work with the Entity Framework


    http://www.PacktPub.com/entity-framework-tutorial/book



    About the Author

    Joydip Kanjilal is a Microsoft MVP in ASP.NET. He has over 12 years of industry experience in IT with more than 6 years in Microsoft .NET and its related technologies. He has authored many articles for some of the most reputable sites like,www.asptoday.com, www.devx.com, www.aspalliance.com, www.aspnetpro.com, www.sql-server-performance.com, www.sswug.com, etc. Several of these articles have been featured at www.asp.net—Microsoft's Official Site on ASP.NET. Joydip was also a community credit winner at www.community-credit.com a number of times.

    He is currently working as a Senior Consultant in a reputable company in Hyderabad, INDIA. He has years of experience in designing and architecting solutions for various domains. His technical strengths include C, C++, VC++, Java, C#, Microsoft .NET, Ajax, Design Patterns, SQL Server, Operating Systems, and Computer Architecture. Joydip blogs at http://aspadvice.com/blogs/joydip and spends most of his time reading books, blogs, and writing books and articles. His hobbies include watching cricket and soccer and playing chess.


    Posted by Mike Borozdin on Saturday, January 03, 2009 6:44 AM GMT
    Shout it Kick it!  
    Permalink | Comments (4) | Post RSSRSS comment feed

    Learning Entity Framework

    I suppose you have already heard about the Entity Framework, but probably didn’t have a chance to try it in action. So, you want to learn it. Of course, you can start with MSDN that is indeed is the number one resource .NET developers, you can also find a dozen of tutorials on the Net, but you know it’s always pleasant to have a paper book or perhaps its electronic versions that you can read everywhere, not only in front of your computer.

    Entity Framework Tutorial So, I can recommend you a book that will help you start working with the Entity Framework straight away. The book has a simple title of “Entity Framework Tutorial” that suggests that it focuses strictly on practical appliance of the Entity Framework and contains many samples.

    That is true. You’ll find a lot of useful samples in this book. In fact, you should really treat the book as a big tutorial that you usually find in the Internet, but this time it’s big enough to cover the most aspects of the Entity Framework and be published as a book.

    It is written by Joydip Kanjilal – Microsoft MVP in ASP.NET who is known by his numerous publications on the reputable developers web sites, like www.aspnettoday, www.devx.com, www.aspalliance.com and many others. “Entity Framework Tutorial” is written in plain English and the book is generally not very long, just 228 pages, but is is enough to get started with the Entity Framework.

    The book covers the essential topic including the following:

    • The Entity Framework Architecture
    • Mapping
    • Working with Stored Procedures
    • Ways of querying for data
      • Entity SQL
      • LINQ To SQL
    • Object Service Layer and CRUD operations
    • Finally, there is an example of building an ASP.NET application with the Entity Framework
    • It also gives an introduction to ADO.NET Data Services

    This is a good book in general, although it doesn’t provide the in-depth review of Entity Framework that you can still find on MSDN, instead “Entity Framework Tutorial” enables you to try and realize the power of the Entity Framework.

    You can buy the book on the official web site of Packt Publishing.


    Posted by Mike Borozdin on Thursday, December 25, 2008 2:04 AM GMT
    Shout it Kick it!  
    Permalink | Comments (3) | Post RSSRSS comment feed

    Data Manipulations with Entity Framework

    When learning a new ORM one of the most important and interesting thing is to get to know how you can manipulate data with that tool.Thus, this particular thing is explained in this tutorial.

    There are two ways of accessing data with the Entity Framework:

    • Entity SQL
    • LINQ to SQL

    We will be using the latter in this tutorial.

    But first you need to create a project and generate a model. You can actually create any project you like, however I use the Console Project type in the examples. Second, you need to generate a model, so you go to Project –> Add Component and choose ADO.NET Entity Data Model

    image

    The next operations are quite obvious, you choose the database, select the tables (Employees, Territories and EmployeeTerritories). In the end you should get a graphical representation of the data model. As I have already written you will get just two entities instead of three. Anyway, I suggest the you rename some properties and entities, so that it looks like this:

    image

     

    Retrieving Data

    That’s how you can retrieve the data:

       1: NorthwindEntities objectContext = new NorthwindEntities();
       2:  
       3: var result = from e in objectContext.Employees
       4:              where e.EmployeeID == 6
       5:              select e;
       6:  
       7: Employee employee = result.First();
       8: employee.Territories.Load();
       9: Console.WriteLine(employee.LastName);
      10: foreach (Territory territory in employee.Territories)
      11: {
      12:     Console.WriteLine(territory.TerritoryDescription);
      13: }

    If you have some experience with LINQ to SQL, then you won’t find anything new in this code, except for one thing, I’ll explain below. Basically, you create an instance of object context and perform a query. However, there is one difference between Entity Framework and LINQ to SQL, when working with Entity Framework, you have to explicitly load the data from from the linked tables. Pay attention to line 8, where such an operation is performed. At the same time, you can include related table in a LINQ query:

    var result = from e in objectContext.Employees.Include("Territories")
                 where e.EmployeeID == 6
                 select e;

    Please, note that you specify the name of the entities set in the Include() method, don’t confuse it with the name of the corresponding table.

    Adding Data

    Adding new data isn’t difficult as well. You simply create a new object and then add it and save the changes.

       1: NorthwindEntities objectContext = new NorthwindEntities();
       2:  
       3: Employee employee = new Employee { FirstName = "John", LastName = "Doe" };
       4: objectContext.AddToEmployees(employee);
       5:  
       6: objectContext.SaveChanges();

    Modifying and Removing Data

    However, if you want to modify or remove an object, the object must be attached to the data context, this means that you either retrieve it from a storage or explicitly attach it to the object context by calling the AttachTo() method:

       1: NorthwindEntities objectContext = new NorthwindEntities();
       2:  
       3: Employee employee = new Employee { EmployeeID = 1};
       4: objectContext.AttachTo("Employees", employee);
       5: employee.FirstName = "John";
       6:  
       7: Employee employeeToDelete = new Employee { EmployeeID = 10 };
       8: objectContext.AttachTo("Employees", employeeToDelete);
       9: objectContext.DeleteObject(employeeToDelete);
      10:  
      11: objectContext.SaveChanges();

    In the following tutorials I’ll explain some fundamentals of the Entity Framework that one should be aware of when developing with the Entity Framework.


    Posted by Mike Borozdin on Wednesday, October 01, 2008 2:45 PM GMT
    Shout it Kick it!  
    Permalink | Comments (1) | Post RSSRSS comment feed

    Getting Intrigued by Entity Framework

    If you use LINQ to SQL, you may wonder why Microsoft released yet another ORM (Entity Framework), while they already had one. You may also be confused, because it’s not obvious which one to choose. Anyway, the Entity Framework has its own shiny features that don’t present in LINQ to SQL. Besides, you can still use LINQ to retrieve data with the Entity Framework, in this case, it’s called LINQ to Entities, at the same time you can also use a SQL like language – Entities SQL. In this story I’ll show one particular feature of Entity Framework that certainly must interest many developers.

    Basically, Entity Framework provides a higher level of abstraction than LINQ to SQL. In fact, in hides the data level. So, you deal with objects that represent real entities, not database tables, like if you were using LINQ to SQL. I mean when you generate LINQ to SQL classes, it creates classes for each tables, even though those tables are merely junction tables without any reflection in our real life.

    Let’s have a look at the example, to make things clear.

    In the Northwind database we have tables – Employees and Territories that are linked with each other by using the third one – EmployeesTerritories.

    Northwind

    Entity Framework modeler generates this schema:

    image

    There are a lot of interesting things here. First of all, there is no junction table, instead there are navigation properties that allows us to access the corresponding territories or employees.At the same time, since the Employees table also points to itself, there are the navigation properties (Subordinates and Manager that are responsible for that kind of relations. Second, there are no foreign keys, we have navigation properties instead of them as well.

    Thus, the Entity Framework allows us to deal with objects that clearly represent real life entities, instead of objects that just reflect tables.

    That’s just one of many sexy features of the Entity Framework. I’ll try to cover them in the next articles, moreover I’m going to put a comparison between LINQ to SQL and Entity Framework in terms of programming, because sometimes it’s not so easy to migrate from LINQ to SQL to Entity Framework because of some pitfalls.


    Posted by Mike Borozdin on Sunday, September 28, 2008 3:07 PM GMT
    Shout it Kick it!  
    Permalink | Comments (3) | Post RSSRSS comment feed

    Entity Framework and MySQL

    Although MySQL don't seem to be very interested in providing a native support for LINQ to MySQL and you have to use 3-rd party providers to interact with MySQL by using LINQ, they seem to move towards Entity Framework. They are holding a webinar about using Entity Framework with MySQL on September, 30.

    I think this can be a one reason of many why it's worth using Entity Framework, the support by database vendors is a great thing. It is not the only reason, of course, there are many other ones. The main thing is that Entity Framework provides a higher level of abstraction than LINQ to SQL, for instance. It can be a topic for a series of articles, so I think, I'll cover Entity Framework in other articles.


    Posted by Mike Borozdin on Wednesday, September 10, 2008 12:57 PM GMT
    Shout it Kick it!  
    Permalink | Comments (2) | Post RSSRSS comment feed