在实体框架中将tinyint映射为布尔值 [英] Mapping tinyint to boolean in Entity Framework

查看:127
本文介绍了在实体框架中将tinyint映射为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定您之前已经看过这个问题。但是,至少在我的搜索中,每次被问到这样的答案: https://stackoverflow.com/a/4017148/ 219838 弹出。答案是完全正确的,但没有涵盖一种情况。如果您的DBA选择使用tinyint列(根据他的说法,位列不可索引)来表示布尔值,而世界上没有任何事物会使他改变这种情况怎么办?

I'm sure you saw this question before. But, at least in my search, every time it is asked an answer like this: https://stackoverflow.com/a/4017148/219838 pops out. The answer is perfectly right, but doesn't cover one scenario. What if your DBA chose to use tinyint columns (according to him bit columns aren't indexable) to represent booleans and nothing in the world will make him change that?

I了解布尔值和字节值之间的区别。但是我不在乎!我需要知道的是:是否有任何解决方法可以将tinyint列映射为布尔值而不是字节?

I understand the difference between the boolean and byte values. But I DON'T CARE! What I need to know is: Is there ANY workaround to map a tinyint column to a boolean instead of a byte?

我在这里发现了一个丑陋的描述: http://www.saulovallory.com/how-to -map-byte-columns-to-bool-in-entity框架,但它不适用于映射。现在我需要一个。

I found an ugly one describe here: http://www.saulovallory.com/how-to-map-byte-columns-to-bool-in-entityframework but it doesn't work for fluent mapping. And now I need one which does.

推荐答案

EF尚未在其中构建自定义类型转换器,例如 IUserType nHibernate。可能的解决方法是将列映射到类型为 byte 的匹配属性,并为另一个具有转换逻辑的 bool 属性。因为这不是映射的属性,所以您将无法在LINQ查询中使用它。

EF does not have built it custom type converters like IUserType in nHibernate. Possible workaround would be to map the column to a matching property with byte type and have another bool property with conversion logic. Because this is not a mapped property you will not be able to use it in LINQ queries.

public class MyClass
{
    public byte DatabaseColumnName { get; set; }

    [NotMapped]
    public bool DomainPropertyName 
    { 
       get
       {
            //conversion logic using DatabaseColumnName 
       }
       set
       {
           //conversion logic using DatabaseColumnName 
       }
}

这篇关于在实体框架中将tinyint映射为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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