Use a struct in place of a primitive for a EF4 property type
Asked Answered
U

2

8

I've got an EF4 entity (code-first) that includes an int bitmask. I've created a Bitmask struct to make working with bitmasks easier (provides bool properties to access the bits). The bitmask struct includes overloaded implicit operators for converting to and from an int.

I tried setting the property type to the bitmask struct but the value is coming back as 0. I know the value in the database has a value and the bitmask works in my unit tests. I set the HasColumnType to "INT".

The property...

[Required]
[Display(Name = "Display Pages Bitmask")]
[Column(Name = "fDisplayPagesBitmask")]
public DisplayPagesBitmask DisplayPagesBitmask { get; set; }

From the context object...

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<Website>()
        .Property(m => m.DisplayPagesBitmask)
        .HasColumnType("INT");
}

Is this possible? If so, what do I need to do to get it to work?

Ultrasonic answered 13/1, 2011 at 1:6 Comment(1)
This is possible with ef-core 8. Take a look at my answers in #75956658 or #51601185Thaumatology
P
4

You can't map your structure directly. You have to map int property (make setter internal or protected) and provide second non mapped property (use NotMappedAttribute or Ignore method) of your custom type which internally sets mapped integer property.

Paulenepauletta answered 13/1, 2011 at 8:47 Comment(2)
Any news on this subject? I have a struct-type property that encapsulates a plain int. Anything changed since 2013?Tortosa
yes also looking for how you go from class to structsElectroencephalograph
H
0

I used a calculated property struct to get to the properties which works with SQLite in Entity Framework 6. Access modifier protected for the ForSQLite properties did not work for me. Even though they should be private or protected in my eyes.

    public Boolean ZystostatikaForSQLite {
        get;
        set;
    }
    public Boolean ImmunsupressivaForSQLite {
        get;
        set;
    }
    public Boolean AntikoagolanzienForSQLite {
        get;
        set;
    }
    public Boolean GlucokortikoideForSQLite {
        get;
        set;
    }
    // 4 Kategorien der Arzneimittel: Zytostatika, Immunsupressiva, Antikoagolanzien, Glucokortikoide
    public struct PharmaceuticalCategories {
        public Boolean Zystostatika;
        public Boolean Immunsupressiva;
        public Boolean Antikoagolanzien;
        public Boolean Glucokortikoide;
    };
    public PharmaceuticalCategories medicineTaken {
        get {
            PharmaceuticalCategories pc = new PharmaceuticalCategories();
            pc.Zystostatika = this.ZystostatikaForSQLite;
            pc.Immunsupressiva = this.ImmunsupressivaForSQLite;
            pc.Antikoagolanzien = this.AntikoagolanzienForSQLite;
            pc.Glucokortikoide = this.GlucokortikoideForSQLite;

            return pc;
        }
        set {
            this.ZystostatikaForSQLite = value.Zystostatika;
            this.ImmunsupressivaForSQLite = value.Immunsupressiva;
            this.AntikoagolanzienForSQLite = value.Antikoagolanzien;
            this.GlucokortikoideForSQLite = value.Glucokortikoide;
        }
    }
Hadsall answered 8/3, 2016 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.