How to query an own created partial property

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
mm2
Posts: 3
Joined: Tue 29 Nov 2011 13:05

How to query an own created partial property

Post by mm2 » Tue 29 Nov 2011 13:32

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;
                }
            }
        }
    }

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Thu 01 Dec 2011 09:11

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;

mm2
Posts: 3
Joined: Tue 29 Nov 2011 13:05

Post by mm2 » Thu 01 Dec 2011 15:36

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?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Fri 02 Dec 2011 16:15

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.

mm2
Posts: 3
Joined: Tue 29 Nov 2011 13:05

Post by mm2 » Mon 05 Dec 2011 07:52

Thank you for your help!

Post Reply