Tag Archives: LINQ

Entity SQL and spatial data

When the Entity Framework was first introduced it came with (basically) two flavors of querying. LINQ to Entities and Entity SQL (ESQL). I’m not going to describe the history here. But as LINQ (any LINQ) gained popularity it was obvious, that LINQ to Entities is a future (though it had initially small deficit compared to Entity SQL). I even on my courses recommend using LINQ to Entities whenever possible and I’m also showing some dynamic querying where normally people start concatenating ESQL strings.

Recently Entity Framework added support for spatial data (and enums, …). All examples I’ve ever seen around spatial data and Entity Framework were using LINQ (obviously). Only few weeks ago I realized how the querying should work with Entity SQL. As it turned out spatial data are not first class citizen in Entity SQL world. There’s no literal for i.e. point. But canonical functions are here to save you. There’s a bunch of these spatial data related. What we’re looking for though is GeometryFromText/
GeographyFromText. With these you can construct Geometry/Geography datatype from well-known text (WKT) and use it in query.

I’ve never used Entity SQL query in any real-world application I created. I’m too afraid of typos etc. But I did a lot of magic with expression trees. :) Wondering what percentage of people using Entity Framework is using Entity SQL and whether there are some scenarios that are crazy hard in LINQ…

ADO.NET provider for Firebird 3.0.1 released

Few weeks ago the 3.0.0 version was released (actually it was during Firebird Conference 2012, during my talk ;) ) and it came with some new features and improvements (hence version 3.0.0). And as it goes, nothing is perfect and few items slipped through testing phase. So that’s th reason why today, I’m happy to announce version 3.0.1 to be released.

You can download it from NuGet or from Firebird SQL site as it gets updated.

This release contains fixes for two regressions introduced in 3.0.0. You can see both in tracker. It’s also my initial step to release these small bugfix releases more often (as discussed in list).

Go grab it, while it’s hot.

How to show SQL command created by Entity Framework? [2012 edition]

Four years back (wow) I wrote a post about how to show SQL command created by Entity Framework. The information there still holds. But now it’s 2012, Entity Framework progressed. Now you’re probably using DbContext and IDbSet<T> APIs, it’s actually recommended.

There you don’t have the ObjectQuery. As the new API is simpler and more focused, also getting the command is simple. Simply call ToString() method and you’re done.

class Program
{
	static void Main(string[] args)
	{
		Database.SetInitializer<MyContext>(null);
		using (var ctx = new MyContext())
		{
			Console.WriteLine(ctx.FooBars.Where(x => x.Id == -1).ToString());
		}
	}
}

class MyContext : DbContext
{
	public IDbSet<FooBar> FooBars { get; set; }
}

class FooBar
{
	public int Id { get; set; }
	public string Baz { get; set; }
}

Union and Concat in LINQ to Entities

Few years back I wrote about “UNION and UNION ALL in LINQ to SQL” and comment there today made me re-think it again. Everything that’s written there is correct. But it might be little bit confusing to see the query being generated from Entity Framework. Actually the query is generated by SqlClient and every 3rd party provider should do the same.

Basically, as written in above mentioned article, Concat generates query, that has the semantic of UNION ALL. Similarly the Union generates query with UNION semantic.

Let’s check that. I’ll use simple Entity Framework’s Code First base set up. And some queries.

class MyContext : DbContext
{
	public MyContext()
		: base(new SqlConnection(@"Initial Catalog=test;Data Source=(localdb)\mssql;Integrated Security=True;Pooling=false;"), true)
	{

	}

	public IDbSet<Test1> Test1 { get; set; }
	public IDbSet<Test2> Test2 { get; set; }
}

class Test1
{
	public int Id { get; set; }
	public int FooBar { get; set; }
}
class Test2
{
	public int Id { get; set; }
	public int FooBar { get; set; }
}
using (var ctx = new MyContext())
{
	Console.WriteLine(ctx.Test1.Select(x => x.Id).Union(ctx.Test2.Select(x => x.Id)).ToString());
	Console.WriteLine(ctx.Test1.Select(x => x.Id).Concat(ctx.Test2.Select(x => x.Id)).ToString());
}

Running this code, produces following queries.

SELECT
[Distinct1].[C1] AS [C1]
FROM ( SELECT DISTINCT
        [UnionAll1].[Id] AS [C1]
        FROM  (SELECT
                [Extent1].[Id] AS [Id]
                FROM [dbo].[Test1] AS [Extent1]
        UNION ALL
                SELECT
                [Extent2].[Id] AS [Id]
                FROM [dbo].[Test2] AS [Extent2]) AS [UnionAll1]
)  AS [Distinct1]
SELECT
[UnionAll1].[Id] AS [C1]
FROM  (SELECT
        [Extent1].[Id] AS [Id]
        FROM [dbo].[Test1] AS [Extent1]
UNION ALL
        SELECT
        [Extent2].[Id] AS [Id]
        FROM [dbo].[Test2] AS [Extent2]) AS [UnionAll1]

Both queries are using UNION ALL. What’s wrong? The key magic is in usage of DISTINCT keyword in first query. That basically turns UNION ALL into UNION. Because UNION is all rows from both sets without duplicates. And DISTINCT will remove these duplicates.

Yes, little bit hidden, but the semantic is kept.

ADO.NET provider 3.0.0 for Firebird and DDEX provider 3.0.0 for Firebird released

I’m pleased to announce version 3.0.0 of ADO.NET provider for Firebird and version 3.0.0 of DDEX provider for Firebird. ADO.NET provider 3.0.0 comes with couple of bug fixes and two major improvements.

  1. Changed transaction isolation modes
  2. New installer that updates machine.config

You can read more about the first at DNET-337. Basically now ReadCommitted and ReadUncommited are same. And using (among others) FbTransactionBehavior.RecVersion/isc_tpb_rec_version. That should better match the default behavior a lot of people is expecting.

The new MSI installer now not only installs all the necessary files, but also updates machine.config file and registers the assembly into GAC (you can select not to do it). So after full install, you don’t have to do anything. You’re just ready to go.

The rest of changes can be seen in tracker log for this version.

The DDEX provider 3.0.0 comes with new installer as well. Now when you do the install, the DDEX provider is fully registered into Visual Studio (you can select version(s) during install) and ready to go as well. No need to fiddle with registry or any other files.

Hope you’ll enjoy these improvements.