在MVC模型中存储枚举值 [英] Storing an enum value in an MVC model

查看:78
本文介绍了在MVC模型中存储枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC模型,

I have an MVC model,

Imports System.ComponentModel.DataAnnotations

Public Class Person
    <key>
    Public Property Id as integer
    Public Property Sex as Gender
End Class

</key>


不幸的是,当框架创建它时,这不会将Sex属性存储到db中.它仅包含id键字段.
作为解决方法,我认为这将是一个很好的解决方案


Unfortunately this does not store the Sex property into the db when the framework creates it. It contains only the id key field.
As a workaround, I thought this would be a good solution

Imports System.ComponentModel.DataAnnotations
Public Class Person
    <key>
    Public Property Id as integer

    <notmapped>
    Private Property sSex as Gender

    Public Property Sex as integer
    Get
        Return sSex
    End Get
    Set(value as integer)
        sSex = value
    End Set

End Class
</notmapped></key>


不幸的是,这破坏了类本身的整个结构,因为我想将性别"用作性别而不是整数.反正有强迫MVC3自动将Enum值存储为整数吗?


Unfortunately this ruins the whole structure of the class itself, as I''d like to use the Sex as a gender rather than as an integer. Is there anyway to force MVC3 to store the Enum value as integer automatically?

推荐答案

数据库数据类型不支持枚举类型.

因此,当保存您的枚举类型时,它在数据库级别没有对应的对应关系,因此最好为枚举设置整数属性.
Database data types are not supporting enum types.

Thus when it is saving your enum type it does not have any counter part at database level to map with so it is better to make integer property for enum.


这篇关于在MVC模型中存储枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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