Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve single Entity Framework entities using a LINQ query or GetObjectKey?

It looks like GetObjectKey has the benefit of searching for existing, instantiated objects, and THEN the data store. However, it also seems like you lose some of the strong typing, and need to cast your resulting object:

GetObjectKey

int customerID = 1;
EntityKey key = new EntityKey("MyEntities.Customers", "CustomerID", customerID);
Customer customer = context.GetObjectByKey(key) as Customer;

vs. LINQ

int customerID = 1;
Customer customer = (from c in context.Customers 
                     where c.CustomerID = customerID
                     select c).FirstOrDefault();

Personally, I prefer the latter method, because of the typing. Also, your DAL will be fairly uniform with all of the Get methods being queries, although that's just a personal preference.

What do you boys and girls use?

like image 768
John B Avatar asked Jun 26 '09 15:06

John B


People also ask

Can we use LINQ with Entity Framework?

LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. Queries against the Entity Framework are represented by command tree queries, which execute against the object context.

Which is better Entity Framework or LINQ to SQL?

First off, if you're starting a new project, use Entity Framework ("EF") instead of Linq to SQL because it now generates far better SQL (more like Linq to SQL does) and is easier to maintain ("L2S").

Which of the following query syntax can be used to query in ef6?

You can use the LINQ method syntax or query syntax when querying with EDM.

Is Entity Framework and LINQ the same?

Entity framework is ORM Model, which used LINQ to access database, and code is autogenerated. It enables developers to work with data using objects of domain specific classes without focusing on the underlying database tables and columns where this data is stored.


2 Answers

I prefer the latter because it is explicitly clear what it is you want. By using EntityKey (and this is something that the ADO.NET team doesn't seem to understand), we have to work around the structure imposed on us by Entity Framework. By using the query language in the way you did in the second example, we're telling all of the rest of the developers who will ever look at our code, hey, we just want this object with this ID or we want null.

I don't think that being correct (as you are in the first example as well) is an excuse for not being clear to your colleagues. :)

like image 145
Rob Avatar answered Sep 23 '22 16:09

Rob


In my solution, I use generic programming. In the base Repository class I have code like this:

private string GetEnittySetName(string entityTypeName)
{
    var container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
    string entitySetName = (from meta in container.BaseEntitySets
                            where meta.ElementType.Name == entityTypeName
                            select meta.Name).FirstOrDefault();
    return entitySetName;
}

private string entitySetName;

protected string EntitySetName
{
    get
    {
        if (string.IsNullOrEmpty(entitySetName))
        {
            entitySetName = GetEnittySetName(typeof(T).Name);
        }
        return entitySetName;
    }
}

public T SelectOne(Func<T, bool> exp)
{
    return context.CreateQuery<T>(EntitySetName).Where(exp).FirstOrDefault();
}
like image 40
finlay Avatar answered Sep 22 '22 16:09

finlay