使用结构体代替 EF4 属性类型的基元

发布于 2024-10-11 14:30:47 字数 657 浏览 7 评论 0原文

我有一个包含 int 位掩码的 EF4 实体(代码优先)。我创建了一个 Bitmask 结构,以便更轻松地使用位掩码(提供 bool 属性来访问位)。位掩码结构包括用于与 int 相互转换的重载隐式运算符。

我尝试将属性类型设置为位掩码结构,但该值返回为 0。我知道数据库中的值有一个值,并且位掩码在我的单元测试中有效。我将 HasColumnType 设置为“INT”。

属性...

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

来自上下文对象...

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

这可能吗?如果是这样,我需要做什么才能让它发挥作用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘人皮目录ツ 2024-10-18 14:30:47

您无法直接映射您的结构。您必须映射 int 属性(使 setter 为内部或受保护),并提供自定义类型的第二个非映射属性(使用 NotMappedAttributeIgnore 方法),该属性在内部设置映射的整数属性。

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.

衣神在巴黎 2024-10-18 14:30:47

我使用计算属性 struct 来获取与 Entity Framework 6 中的 SQLite 配合使用的属性。
ForSQLite 属性保护的访问修饰符对我不起作用。即使它们在我眼中应该是私人的或受保护的。

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

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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文