More RIA Domain Service difficulties with EF

clock April 3, 2009 03:45 by author Giovanni

Another error I recently ran into with RIA Domain Services and the Entity Framework model:

The type 'ProjectName.Web.YourType' already contains a definition for '_fieldName'
 
This is usually followed immediately by a second error (described the same way) except it shows the property name.  This seems to occur when a navigation link in EF is named exactly the same as the internal SQL Server column name.  For example, let's say you have a table with a field/column named ModifiedBy (represented as an int, and is a foreign key to another table), when the table is added to the EF model, it is represented as a navigation link.  If you decide to rename the link to "ModifiedBy", EF will allow this, but when RIA generates metadata for the client side, it will complain because, in the metadata, it internally creates both - the object for the navigational link as well as the internal type of the field.


Problem with casting enums in LINQ to Entities

clock November 7, 2008 05:55 by author Giovanni

I've been working with the Entity Framework lately, and I tried to do a LINQ query that returned a record/object with an id that was equal to a value represented by an enum type.  I ended up getting the following error: "Unable to create a constant value of type 'Closure.type'.  Only primitive types ('such as Int32, String, and Guid') are supported in this context."
The code looked similar to below:

public void Test(MyEnum mEnum)

{ var s = (from mt in MyContext.MyTable where (mt.ID == (int) mEnum) select mt).FirstOrDefault(); }

Even though we are casting mEnum before evaluating it, Linq to Entities has a problem. 

 

The solution is to create an int variable prior to running the query as shown below:

public void Test(MyEnum mEnum)
{
    int myEnumValue = (int) mEnum;
    var s = (from mt in MyContext.MyTable where (mt.ID == myEnumValue) select mt).FirstOrDefault();
}
 
Based on this blog post I found: Matt Hidinger's Blog Post I found out why you cannot do high level comparisons (objects) or even functions that return primitives, so I guess we can extend this to say the casting of an enum doesn't work either!