views:

121

answers:

2

Given a simple POCO class such as:

public class User { public int Id { get; set; } public string Name { get; set; } public string Password { get; set; } }

When I try a query with projection to itself like so:

using (var context = new Context())
{
var user = context.User.Select(u => new User { Id = u.Id }).FirstOrDefault();
}

... I get:

Unhandled Exception: System.ArgumentException: 'Id' is not a member of type 'ORMTest1Model.Users'

... wich comes from the method ValidateMemberInitArgs(...) from System.Linq.Expressions.Expression (use Reflector).

In this method, the type from binding.Member.DeclaringType is of type PocoAdapters.UserAdapter (the generated one) and the type from variable "type" is of type User (the POCO class).

So... for some reason, it's mixing thing up.

Interestingly, if I create a class MyUser which is the exact copy of the poco class User, it works fine and both types at ValidateMemberInitArgs(...) are of type MyUser.

Can anyone reproduce the issue and shed a light on the solution?

Thanks!

(link to same question in project's discussion list: http://code.msdn.microsoft.com/EFPocoAdapter/Thread/View.aspx?ThreadId=2138)

A: 

I think the problem is how the class User is being resolved.

To confirm, can you refactor POCO class to be named PocoUser. Does the same error occur?

var user = context.User.Select(u => new User { Id = u.Id }).FirstOrDefault();

would become

var user = context.User.Select(u => new PocoUser { Id = u.Id }).FirstOrDefault();

HTH,

Dan

Daniel Elliott
A: 

I think I figured out why this is happening.

MsSql allows for capital letters in table names, but MySql does not. So if you create your poco adapter from an mssql entity model which contains capital letters in table names, and use it to query a mysql database, you will get this type of error.

In order to fix this, I just renamed my tables to all lowercase.

Rami A.