简体   繁体   English

Telerik OpenAccess:是否可以映射“ NullAble”枚举类型属性?

[英]Telerik OpenAccess:Can a “NullAble” enum type property be mapped?

I am a beginner of OpenAccess.I am writing some test code according to my experience of "Linq to Sql" 我是OpenAccess的初学者。根据“ Linq to Sql”的经验,我正在编写一些测试代码。

I try to map a allow null int field to "NullAble" enum type property, but fail. 我尝试将allow null int字段映射到“ NullAble”枚举类型属性,但是失败。

In the Visual Designer of VS.Net, if I set the "Nullable" property to "true" and "Type" property to "MyEnum"("MyEnum" is the enum I defined) to the property in the domain class, the error below will be returned when I compile the project. 在VS.Net的可视设计器中,如果将Domain类中的属性的“ Nullable”属性设置为“ true”,将“ Type”属性设置为“ MyEnum”(“ MyEnum”是我定义的枚举),则错误编译项目时,将返回以下内容。

The type for member with name "MyEnum" of "MyClass" presistent class is not a valid mapping for its column. 名称为“ MyClass”持久类的名称“ MyEnum”的成员的类型对其列无效。

If I set the "Nullable" property to "false" and "Type" property to "MyEnum?" 如果我将“ Nullable”属性设置为“ false”,而“ Type”属性设置为“ MyEnum?” to the property in the domain class, the compiling pass, but the error below will be returned when fetch data from database at runtime. 传递给domain类中的属性,编译通过,但是在运行时从数据库中获取数据时,将返回以下错误。

Type converter initialization failed. 类型转换器初始化失败。 The converter with name 'IntConverter' does not convert from CLR type 'System.Nullable`1[[Model.MyEnum, Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' to SQL type 'int'. 名称为'IntConverter'的转换器不会从CLR类型'System.Nullable`1 [[Model.MyEnum,Model,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]'转换为SQL类型'int'。

But in "Linq to Sql", "NullAble" enum type property can be mapped into a class and work fine. 但是在“ Linq to Sql”中,“ NullAble”枚举类型属性可以映射到一个类中并且可以正常工作。

Thank you for any help in advance! 预先感谢您的帮助!

Unfortunately it seems that the Nullable Enums are not currently supported but this issue is already addressed and you could find it updated in a future version of Telerik OpenAccess ORM. 不幸的是,目前似乎不支持Nullable枚举,但是此问题已得到解决,您可以在Telerik OpenAccess ORM的将来版本中对其进行更新。

As a workaround you could create a wrapper property in a partial class of your Domain Class for getting and setting the Enum properly. 解决方法是,可以在域类的子类中创建包装器属性,以正确获取和设置枚举。

You could achieve this by following the steps below: 1) Change the type of the Domain Class Property to "int", set its Nullable setting to "true" and its Access Modifier to "private". 您可以通过执行以下步骤来实现:1)将Domain Class属性的类型更改为“ int”,将其Nullable设置设置为“ true”,并将其访问修饰符设置为“ private”。 2) Create a partial class of the entity containing the Nullable enum. 2)创建包含Nullable枚举的实体的部分类。 3) Add a wrapper property and implement its getter and setter as below: 3)添加包装器属性并实现其getter和setter,如下所示:

public MyEnum? MyEnumWrapper
{
    get
    {
        if (this.MyEnum.HasValue)
        {
            return (MyEnum)this.MyEnum.Value;
        }

        return null;
    }
    set
    {
        this.MyEnum = (int?)value;
    }
}

Regarding the mapping error that you are getting - it is a part of the OpenAccess ORM validation framework which is meant to warn you about possible inconsistencies in the model and it does not mean that your project is not buildable and it will also be fixed implementing the nullable enum support. 关于您遇到的映射错误 -这是OpenAccess ORM验证框架的一部分,该警告旨在警告您模型中可能存在的不一致,并不意味着您的项目不可构建,并且也将通过实施该版本进行修复可为空的枚举支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM