代替原始的使用结构为EF4属性类型 [英] Use a struct in place of a primitive for a EF4 property type

查看:125
本文介绍了代替原始的使用结构为EF4属性类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EF4实体(code-第一),其中包括一个int位掩码。我创建了一个位掩码结构,使同位掩码容易(提供布尔属性来访问位)工作。该位掩码结构包括重载的隐性运营商转变,并从一个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.

我尝试设置属性类型的位掩码结构,但该值是回来为0。我知道在数据库中的值有一个值和位掩码工作在我的单元测试。我设置了HasColumnType为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".

属性...

[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?

推荐答案

您不能直接映射你的结构。你必须映射INT财产(使二传手内部或保护),并提供第二不对应的属性(使用 NotMappedAttribute 忽略您的自定义类型的方法)在内部设置映射整型属性。

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.

这篇关于代替原始的使用结构为EF4属性类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆