Archive for the ‘Entity Framework’ Category

EF + NPGSQL: “Only PrimitiveTypes can be used without qualification.”

01:46 PM

Are you trying to create a SSDL/EDM file for Entity Framework and Npgsql ? Are you getting the following error?

error 0040: The Type … is not qualified with a namespace or alias. Only PrimitiveTypes can be used without qualification.

This is caused by Npgsql mapping a very sparse selection of type-names to the underlying Entity Framework type structures.

As of 2.0.9, the following type names are recognized:

  • bool
  • int2
  • int4
  • float4
  • float8
  • uuid
  • numeric
  • bpchar
  • varchar
  • text
  • xml
  • timestamp
  • date
  • bytea

Dynamic search with Linq

05:50 PM

The Problem: Find all sentences with either “fox” and “dog” and “fox” or “cat”.


string[] sentences = new string[] {
  "The quick brown fox jumps over the lazy dog",
  "The quick brown fox jumps over the lazy cat"};
string[] findAnd = new string[] { "fox", "dog" };
string[] findOr = new string[] { "dog", "cat" };

The first part (find all) is simple:


var queryand = sentences.AsQueryable();
foreach (string find in findAnd)
{
  string f = find;
  queryand = queryand.Where(s => s.Contains(find));
}

Console.WriteLine(string.Join("\n", queryand.ToArray()));

But how to implement to Or part? Well, google is my best friend. I found the following solution:

Predicate Builder on C# 3.5 in a Nutshell

But this solution does not work with Entity Framework: Predicate Builder and Entity Framework on stackoverflow.com

After implementing this solution your code will look like this:


var predicate = Extensions.False<string>();
foreach (string find in findOr)
{
  string f = find;
  predicate = predicate.Or(s => s.Contains(f));
}
var queryor = sentences.AsQueryable().Where(predicate);
Console.WriteLine(string.Join("\n", queryor.ToArray()));

P.S.: You can find an ExpressionVisitor here: ExpressionVisitor at MSDN

Unbelievable!

08:13 PM

Wow! Automatic “func eval” in Visual Studio is cool. You can even do more cool things with DebuggerDisplay (e.g. “rating = great”). If a getter has to do too much work for a debugging session to get his job done you can suppress evaluation by adding a DebuggerBrowsable attribute.

But the nice small Debug Windows “Auto” and “Locals” ignore these attributes. So your debugger will continue to do unnecessary work – an amazing feature. This is especially bad since there is a timeout within the debugger’s evaluator which will kill the debugger to the point of unusability when hit.

And yes – this is by Design:

Entity Framework Designer v1…

12:06 PM

Usually I like Entity Framework. And as I have to write the EDMX files by hand (for several reasons) I didn’t had any problems with mapping my Database and Objects.

But for another project I would like to use the designer. And I would like to use Complex Types. This is a situation where I dont like Entity Framework (or better: the Designer).

Error: ComplexType elements are not supported in the Entity Designer.

Yep – this is a known issue:

So I’ll try not to use Complex Types (with some bad tricks this sould be possible) or I will continue writing EDMX Files by hand…

1:1 Relations and Entity Framework

09:02 AM

Yesterday I tried to declare a 1:1 Relation in Entity Framework. As I have to write the EDMX File by hand (for various reasons) I’ve tried a straight forward way and ran into some problems. But first let me show you the sample Model:

(more…)

Entity Framework: EntityReference.IsLoaded

04:01 PM

There is an issue with the Entity Framework Beta 3. If you use the EntityReference Object’s Load() Method and the Result is null then the IsLoaded bit is not set.

This is nasty because the EF will always try to load the Reference from the Database.

Or I made a mistake…

Here is an example:


[EdmRelationshipNavigationPropertyAttribute("Model", "FK_Auftrag_Mitarbeiter", "A_Mitarbeiter")]
public App.Mitarbeiter Mitarbeiter
{
  get
  {
    EntityReferenceMitarbeiter> r =
      ((IEntityWithRelationships)(this)).RelationshipManager
      .GetRelatedReferenceMitarbeiter>
      ("Model.FK_Auftrag_Mitarbeiter", "A_Mitarbeiter");

    if (!r.IsLoaded)
      r.Load();

    return r.Value;
  }
}

Entity Framework: How to embed C/M/S Files

01:12 PM

That’s simple:

  1. Embed your files as a resource in your DLL
  2. In your app.config change the connectionstring from:

<add name="KistlContext"
  connectionString="metadata=.\Model.csdl|.\Model.ssdl|.\Model.msl;
  provider=System.Data.SqlClient;
  provider connection string='Data Source=.\SQLEXPRESS;
    Initial Catalog=YourDatabase;
    Integrated Security=True;
    MultipleActiveResultSets=true;'"
  providerName="System.Data.EntityClient" />

to:


<add name="KistlContext"
  connectionString="metadata=res://*;
  provider=System.Data.SqlClient;
  provider connection string='Data Source=.\SQLEXPRESS;
    Initial Catalog=YourDatabase;
    Integrated Security=True;
    MultipleActiveResultSets=true;'"
  providerName="System.Data.EntityClient" />

It does not matter in which assembly the model is embedded.