Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL DataContext.Translate and properties with name different than the source

I am working with some tables where I want the C# class to have a different property name than the underlying table column. However, when I use the Translate method to read the results, the properties that don't match the source name never get populated. Even when I use Linq to generate the SQL.

For instance, my table is defined in the DB like this:

CREATE TABLE User_Entry (
    UserId int IDENTITY (1, 1) NOT NULL,
    Login_Id varchar (50) NOT NULL,
    Active char(1) NOT NULL,
    PASSWORD varchar(75) NULL
)

Here's the class it maps to (generated by the LINQ designer...LINQ attributes and other stuff left out for brevity):

public partial class User
{
    int UserId;
    string Login;
    string Active,
    string Pwd
}

When I do the following, the Login and Pwd properties are not populated but the UserId and the Active properties are.

Data.DbContext db = new Data.DbContext();

IQueryable query = db.Users.Where(usr => usr.Login == request.LoginString);

SqlCommand cmd = (SqlCommand)data.GetCommand(query);

... execute the command (asynchronously) ...

User user = db.Translate<User>( dataReaderResult ).FirstOrDefault();

!At this point, I inspect the user object and I can see that the Login and Pwd columns are not populated!

Here is the sql that was generated:

exec sp_executesql N'SELECT [t0].[UserID] AS [UserId], [t0].[Login_ID] AS [Login], [t0].[Active], [t0].[PASSWORD] AS [Pwd]
FROM [dbo].[User_Entry] AS [t0]
WHERE [t0].[Login_ID] = @p0', N'@p0 varchar(13)', @p0 = 'test_user'

Originally, when the UserId column had a different property name I was getting an exception The required column [UserId] does not exist in the results. I looked around and I saw a response from some MSFT people that said it was a bug.

Is this related? Was it ever fixed? Does anyone know when it will be fixed?

Edit: Some more info.

The bug that I think this is related to has a comment from Kathy Lu MSFT on 14 Aug 2007 here, where she says:

Thank you for reporting this issue. From your issue we were able reproduce the issue and the product team is looking into an appropriate triage and resolution.

I'm wondering if this is related to what I'm experiencing and if I can get more info about it. I searched linq translate site:connect.microsoft.com but I didn't find anything.

like image 260
Wayne Bloss Avatar asked Oct 17 '08 00:10

Wayne Bloss


1 Answers

Are the column attributes set up properly? There is a name property.

http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.columnattribute_members.aspx

Also, Translate's documentation... you want to be mapped (as in the first bullet point).


The algorithm for matching columns in the result to fields and properties in the object works as follows:

  • If a field or property is mapped to a particular column name, that column name is expected in the resultset.
  • If a field or property is not mapped, a column with the same name as the field or property is expected in the resultset.
  • The comparison is performed by looking for a case-sensitive match first. If this match is not found, a subsequent search is occurs for a case-insensitive match.

Oh wait, you're using the generic Translate overload. Try the Translate overload using the Type parameter and see if that treats you better.

like image 104
Amy B Avatar answered Nov 10 '22 00:11

Amy B