9

When I add extra properties to a EF auto-generated class by using an extra partial class, these properties are not populated or filled when running queries against the database.

Example:

Auto-Generated class Person:

public partial class Person
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

My own partial class

public partial class Person
{
    public string DisplayName{ get; set; }
}

When I make the following query:

"SELECT *, (FirstName + LastName) AS DisplayName FROM [Person]" 

and use

DbContext.Database.SqlQuery(typePerson, SQL, null)

the Id, FirstName and LastName are populated but not the DisplayName.

However when I create a whole new class called MyPerson

public partial class MyPerson
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName{ get; set; }
}

And run the same query with the type of MyPerson the DisplayName is populated as well.

Can anyone explain this or tell me how I can fix this problem so I can use Partials instead of having to create new classes/types.

Download an example: https://www.dropbox.com/s/dayrv0jzuoju9q3/StackOverflow_EF_ExtraProperties.zip?dl=0

UPDATE 2015-12-28: While reading through other stackoverflow and Codeproject forums I found another ways to get it to work:

1) Using Typebuilder (http://www.codeproject.com/Articles/206416/Use-dynamic-type-in-Entity-Framework-SqlQuery) but due to all sort of project dependencies I had problems finding already created types and not blowing up the memory;

2) Best option so far: using inheritance.

When I create another class with just this lines:

class Person_Reflect : Person { }

I can use the following code (it will ignore the mapping in the EDMX file and use reflection:

List<EF.Person> listPerson = dbEntities.Database.SqlQuery<EF.Person_Reflect>(sql, new object[] { }).ToList<EF.Person>();
7
  • 3
    Are you sure the 2nd partial class of Person is in the right namespace? Commented Dec 24, 2015 at 8:17
  • And no exception since you have changed the model? Commented Dec 24, 2015 at 8:23
  • Do you see DisplayName as a property without value? Commented Dec 24, 2015 at 8:32
  • Thx for all your comments! Yes in the same namespace. "No exception"? Can you elaborate on your comment? Yes I see the Displayname as a property with value NULL. Commented Dec 24, 2015 at 9:13
  • I have never tried Model-first with EF. But for this particular problem, I would make: public partial class Person { public string DisplayName{ get { return FirstName + LastName; } } } Commented Dec 24, 2015 at 11:48

2 Answers 2

2

Best option so far without having to code much and or to do much code review when Database changes occur is to use inheritance.

When I create another class with just this line:

class Person_Reflect : Person { }

I can use the following code (it will ignore the mapping in the EDMX file and use reflection):

List<EF.Person> listPerson = dbEntities.Database.SqlQuery<EF.Person_Reflect>(sql, new object[] { }).ToList<EF.Person>();
Sign up to request clarification or add additional context in comments.

Comments

0

try remove the *

@"
 SELECT 
     Id 
    ,FirstName 
    ,LastName 
    ,(FirstName + ' ' + LastName) AS DisplayName 
 FROM [Person]" 

Alternatively you could do this is code... and then you would not need to include it in your raw sql

public string DisplayName { get { return FirstName+ " " +LastName ; } }

5 Comments

Leaving out the * doesn't work, it even makes it worse; if I forget one column the application ends with the exception: The data reader is incompatible with the specified 'Application.Person'. A member of the type, 'COLUMNNAME', does not have a corresponding column in the data reader with the same name. You are absolutely right, your coding solution solves this example but I have a dynamic query that can be enriched on runtime to create a multitude of end user choices of how to display/format results. So I only have a view options: using evals or creating dynamic queries.
it should work... the column must be the same CASE and data type. I have done something simular myself so i know it works.. please double check case and types
Dear Seabizkit. I have uploaded a test example in which I can't seem to get it to work what you have accomplished. The link to the full example is: dropbox.com/s/dayrv0jzuoju9q3/…
FFS why was this down voted. OMG really are people so.... ish @LiQuick.net ill double check myself later.... when im back at work.
Sorry for the downvoting not my doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.