Entity Framework Issues

February 4, 2009 08:59 by bryan

I've come across two issues with the Entity Framework, one that effects any language and one just for VB.NET

It's quite simple, the Entity Framework generates edmx files, however that Inherits Global.System.Data.Objects.DataClasses.EntityObject, which means when you create your Partial class you can not Inherit from your own base class.

The second issue is related to VB.NET, as if you are intending to use Dependence Injection or IoC you need to have an Interface defined and to implement an Interface in VB.NET you must add Implements after each Property and Method, which you can not do as the framework generates it's own code.

I've attached both C# and VB.NET examples, none of which compile

Entity Framework Issues.zip (42.58 kb)


Creating an Entity Connection for the Entity Framework

January 23, 2009 09:18 by bryan

When you start to play with the Entity Framework you will soon come up with the need to store your connection in a common place.  To do this you will need to with provide the connection string to the Entity Object Context or provide the Entity Connection.  I prefer to us the Entity Connection as you can explicity define each section of the connection.

C#

VB.NET

and if you need to pass in the UserID and Password

For some more help on setting this up check out Ricka on Dynamic Data


LINQ to Entity Framework

January 21, 2009 14:19 by bryan

What with Entity Framework around, I have been finding it quite hard to find any good information about the Framework, so I have started to compile a list of good resources

The ADO.NET Entity Framework Overview

Code Samples

Mike Taulty is Mr Entity Framework

Professional ADO.NET 3.5 with LINQ and the Entity Framework

Entity Framework Toolkits & Extras

Transparent Lazy Loading for Entity Framework


LINQ to SQL v LINQ to Entities

January 19, 2009 14:43 by bryan

Whatare the differences between LINQ to SQL v LINQ to Entities, well:

LINQ to SQL uses tables as entities

LINQ to Entities allows me to write C# LINQ queries that run against a conceptual data model.

The following table provides a summary of the features within each LINQ object

So this means that LINQ to SQL uses a database model to create its entities, where as the Entity Framework work on a layer of abstraction above the data, this is a most important feature.  As most database changes are taken care of by the schema and mapping without requiring a change to the obect model - making it so you do not have to refactor and rebuild your objects.

The second difference is the Entity Framework has the ability to allow the entity inheritance and entity composition.  This means that you can create an entity that is composed of colums originating in multiple tables without any complex join logic.

In summary:

  • If you want the added security of insulation and loose coupling from the underlying database schema to make your object model more resilient to change, use the Entity Framework
  • If you find that you need the features of entity inheritance and entity composition, use the Entity Framework
  • If you already have a large LINQ to SQL codebase that is running just fine without entities, you probably don't need to spend the time to refactor out LINQ to SQL to replace it with LINQ to Entities.
  • If you want to run LINQ queries against an object model, but your object model is a 1:1 mirror of the tables in your database, you probably don't need the Entity Framework.


LINQ Entity Framework

January 19, 2009 11:37 by bryan

Microsoft has released it's Entity Framework to the work, so does this mean the death of LINQ to SQL?  Check out my blog on this, Death of LINQ to SQL

So why is LINQ so good, well the problems with data has always been around the different and many way you need to know to interface and programme to.  For example if you conntect to a Microsoft SQL Server database, you need to know SQL, to read and write to XML files you need to learn XPath or XQuery, to query Active Directory you need to know LDAP (Lightweigth Directory Access Protocol).  To know all of these are more is quite a task.

SO along comes LINQ, which is a intergrated query language, which provides the same query language and grammer features, no matter where the data is held.

Microsoft started off by allowing developers to use LINQ to XML, LINQ to L, and LINQ to Objects.  These are all good starting blocks, but I have always been aware that Microsofts intention has been to pull all of these together and under one method, and that is the Entity Framework.  Which is built on top of LINQ and ADO.NET 3.0.

In order to use the Entity Framework you will nedd Visual Studio 2008, along with Service Pack 1 and .NET 3.5

Defining an Entiry Data Model

Once you have created your project, say an Console Application, you can get down to generating an Entity Data Model

 

  1. Make sure you have Visual Studio 2008 SP1, otherwisethe Entity Framework will not be available.
  2. Add a New Item to your project, and select ADO.NET Entity Data Model.
  3. Change the name to read NorthwindModel.edmx
  4. Select to Generate From Database
  5. Choose you data connection you wish to use, select the Northwind database
  6. Save the connection, you should find your app.config should now have the NorthwindEntities
  7. Select the database objects you wish to choose, in ower example select Customer, Orders, Order Details and Products.
  8. Change the Model Namespace to NorthwindModel and click finish.
You should end up with an .EDMX file looking something like this:

Okay, now it is time to query the data

Querying Products

What is nice about LINQ is it looks and feels very much like SQL.  With one exception, you have to write the syntax back to front.  Why I hear you ask, I don't know the official answer to this, but my thoughts are that it is due to intellisense, as by writing you syntax backwards the intellisense can find the information you need.

Okay to query the Products table, you will first need to create an instance of the NorthwindEntities Class, where did this come from?  Well it was created when you created the .edmx file.

 

NorthwindEntities entities = new NorthwindEntities();

Dim db As New NorthwindEntities()

 

Next we have to create the LINQ statement to read the Products table / entity

 

var products = from p in entities.Products

                           where p.ProductID == 7
                           select p;

Dim products = From p In entities.Products _
                       Where p.ProductID = 7 _
                       Select p


Okay now you have the products you can loop through the list by using a For Each method

foreach (var p in products)
{
      Console.WriteLine(p.ProductName);
}

For Each p In products
      Console.WriteLine(p.ProductName)
Next

How simple could it be?

Check out my other blog entries in using LINQ to find out more information on what you can do with LINQ

To find out more about LINQ and the Entiry Framework check out The ADO.NET Entity Framework OverviewThe ADO.NET Entity Framework Overview or Introduction to ASP.NET Dynamic Data


Simple LINQ 2 SQL example

January 15, 2009 13:31 by bryan

I've been using LINQ to SQL for some time, and each time I come back to the same example, so it's about time I shared the example:

 

      //  Create a DataContext.
      Northwind db = new Northwind();

 

      //  Retrieve customer LAZYK.
      Customer cust = (from c in db.Customers
                       where c.CustomerID == "LAZYK"
                       select c).Single<Customer>();

 

      //  Update the contact name.
      cust.ContactName = "John Smith";

 

      try
      {
        //  Save the changes.
        db.SubmitChanges();
      }

      //  Detect concurrency conflicts.
      catch (ChangeConflictException)
      {
        //  Resolve conflicts.
        db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
      }

 


Generating an Audit for LINQ

December 20, 2008 17:37 by bryan

One of the tasks Jon was given was to create an Audit module for LINQ, and came across this link which seems to provide a solution.  I've not had time to go over the solution, but it appears to do a very good job at first glance.

LINQ to SQL Audit Trail 

LINQ Audit Trail v2 - DoddleAudit


LINQ to SQL and Paging

December 15, 2008 13:29 by bryan

Oh how I remember those days of page through data, using a GridView controller, or any other controller, lots of coding and so easy to make a mistake.

Just take a look at http://www.unboxedsolutions.com/sean/archive/2005/12/28/818.aspx by Sean Chase, and you will see what I mean.

Things have moved on a bit since those days, and now we have LINQ it is much easier, both directly feeding from LINQ or via methods, both provide the same result.

Linq DataSource 

Here is how to enable paging directly to LINQ, by using the LinqDataSource

How easy could it be?

Linq Methods 

When it comes to generating your own method you can do this with just a little more coding and take advantange of the Linq Skip and Take methods, the sample below provides an example using a Method. 


  

To use this fully you will need to make a few changes to the webpage, as shown below:

Now that you have everything set on the page all that is left is the code behind and the methods, I am using the partial class in the example, but it does not have to be.

Code Behind

Methods 

 

To ensure that the application is Paging correctly I found running the SQL Profiler on the database made sure that only the data page was being returned and nothing else, you may want to perform your own tests too.

I have included a small application using Visual Studio 2008 and .NET 3.5, all you will need to change is the connection string settings to the NorthWind database. 

GridPerformance.zip (16.46 kb)


DataContext in LINQ to SQL

December 10, 2008 11:26 by bryan

What is the best practice and pattern to use for the DataContext with LINQ to SQL.

While using LINQ to SQL and it's DataContext I have found it is very easy to fall in to a concurrency issues when you try out multiusers, I've not found the answer as yet, but I have come across this article for different methods

Best practice and effective way of using DataContext in Linq to SQL?


LINQ 2 SQL default values

December 9, 2008 13:52 by bryan

One little crip I am having with LINQ to SQL is that when you build the DBML files within Visual Studio 2008, the default values from the database are not taken in to account.

This means that you need to set the default values against your object before it is saved or after the object is created.

This is a pain the the back side. 

This articles provides a method to overcome this "Handling Default Values With LINQ to SQL"