LuqJensen
LuqJensen

Reputation: 310

System.ComponentModel.DataAnnotations.Schema.ColumnAttribute inconsistent?

I seem to have run into a problem with the ColumnAttribute not working as expected.
Now i just want to point out that i have found a solution to my problem by simply using the AS keyword for naming the columns according to the C# properties, but that does not explain what happens to the ColumnAttribute. As you can see in my SQL i applied the AS keyword to translate the column name, but i left some out to show the issue.

Heres the SQL query

var model = adsic.Database.SqlQuery<ADUser>(@"
    SELECT 
    [mail],
    [title] AS Title, 
    [c] AS Country,
    [department] AS Department,
    [company] AS Company,
    [mobile], 
    [telephoneNumber],
    [facsimileTelephoneNumber],
    [name] AS Name,
    [sAMAccountName] AS Username,
    [l] AS Location,
    [wWWHomePage] AS WebSite,
    [manager] AS Manager
    FROM openquery
    (ADSI,'SELECT 
        mail, title, c, department, company,
        mobile, telephoneNumber, facsimileTelephoneNumber, name, sAMAccountName,
        l, wWWHomePage, manager
        FROM ''LDAP://OU=Plus Pack,DC=global,DC=ns''
        WHERE objectClass = ''User'' AND objectCategory = ''person'' '
    )").AsQueryable().ToList();

If i run this directly on the SQL server everything looks fine and i get all fields populated SQL output

The C# model that reflects the query

using System.ComponentModel.DataAnnotations.Schema;
public class ADUser
{
    //[Key]
    //[Column("distinguishedName")]
    //public string DistinguishedName { get; set; }

    [Column("mail")]
    public string Mail { get; set; }

    //[Column("title")]
    public string Title { get; set; }

    [Column("c")]
    public string Country { get; set; }

    //[Column("department")]
    public string Department { get; set; }

    //[Column("company")]
    public string Company { get; set; }

    [Column("mobile")]
    public string MobileNumber { get; set; }

    [Column("telephoneNumber")]
    public string TelephoneNumber { get; set; }

    [Column("facsimileTelephoneNumber")]
    public string DepartmentNumber { get; set; }

    //[Column("sAMAccountName")]
    public string Username { get; set; }

    //[Column("l")]
    public string Location { get; set; }

    //[Column("wWWHomePage")]
    public string WebSite { get; set; }

    //[Column("manager")]
    public string Manager { get; set; }

    //[Column("cn")]
    public string Name { get; set; }
}

Now for the sketchy part. I debug the result after model translation in C#. C# debug session As you can see MobileNumber and DepartmentNumber do not get populated, despite both having a value in the raw SQL above. Whats more is that the other columns with ColumnAttribute Mail, Country, TelephoneNumber work just fine. I already tried several times to check for typos or hidden whitespace characters. If i dont use the ColumnAttribute at all and just use AS keyword for name translation i get all fields populated.

Upvotes: 1

Views: 1009

Answers (1)

LuqJensen
LuqJensen

Reputation: 310

Duplicate of this post I dont know why, i didnt find this when i searched for my issue before i posted. Or why it wasnt suggested when i was writing my post. Its a shame about that really. Still, the behaviour of EF respecting the attribute sometimes and other times not, is really odd.

Upvotes: 1

Related Questions