使用结构体代替 EF4 属性类型的基元
我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法直接映射您的结构。您必须映射 int 属性(使 setter 为内部或受保护),并提供自定义类型的第二个非映射属性(使用
NotMappedAttribute
或Ignore
方法),该属性在内部设置映射的整数属性。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
orIgnore
method) of your custom type which internally sets mapped integer property.我使用计算属性
struct
来获取与 Entity Framework 6 中的 SQLite 配合使用的属性。受
ForSQLite
属性保护的访问修饰符对我不起作用。即使它们在我眼中应该是私人的或受保护的。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.