Page 1 of 1

How to query an own created partial property

Posted: Tue 29 Nov 2011 13:32
by mm2
Hello,
I have a table "Person" and a column "GenderID" (Data Type integer). Also I have added a partial class with the property "Gender". In this property I translate the integer value from the "GenderID" into an enum (0 for "male", 1 for "female" and 2 for "other").
The "GenderID" property is private and I want to work only with the public property "Gender". But how do I map my property "Gender" to the column "GenderID"?

Here is the code:
The query:

Code: Select all

var query = from person in dbData.Persons
                            where person.Gender == Genders.F
                            select person;

The own partial class

Code: Select all

public enum Genders
    {
        M = 0,
        F = 1,
        O = 2
    }

    public partial class Person
    {
        public Genders Gender
        {
            get
            {
                if (GenderID.HasValue)
                {
                    switch (GenderID.Value)
                    {
                        case 0:
                            return Genders.M;
                        case 1:
                            return Genders.F;
                        case 2:
                            return Genders.O;
                        default:
                            return Genders.O;
                    }
                }
                else
                {
                    return Genders.O;
                }
            }
            set
            {
                switch (value)
                {
                    case Genders.M:
                        GenderID = 0;
                        break;
                    case Genders.F:
                        GenderID = 1;
                        break;
                    case Genders.O:
                        GenderID = 2;
                        break;
                    default:
                        GenderID = 2;
                        break;
                }
            }
        }
    }

Posted: Thu 01 Dec 2011 09:11
by Shalex
1. The enum support is not implemented yet in Entity Framework (looking forward to EF 4.5).
2. Linq to Entities doesn't allow to use a computed property which is not mapped to the database column.

Try using

Code: Select all

var query = from person in dbData.Persons 
                            where person.GenderID == (int)Genders.F 
                            select person;
instead of

Code: Select all

var query = from person in dbData.Persons 
                            where person.Gender == Genders.F 
                            select person;

Posted: Thu 01 Dec 2011 15:36
by mm2
Thank you for your answer!

My problem is not the enum.

I want to map my own created property with a database column. In the example the "genderID" property is created by the or-mapper and is mapped automatically to a database column. Now I have created an own property "gender" and I want to map it with the same database column which is mapped with the property "genderID". Is this possible?

Posted: Fri 02 Dec 2011 16:15
by Shalex
mm2 wrote:Now I have created an own property "gender" and I want to map it with the same database column which is mapped with the property "genderID". Is this possible?
Entity Framework doesn't allow to map two properties to the same column in a database.

The enum support is not implemented yet in Entity Framework (looking forward to EF 4.5). Possible solutions are mentioned at http://stackoverflow.com/questions/1526 ... -framework.

Posted: Mon 05 Dec 2011 07:52
by mm2
Thank you for your help!