如何将枚举映射为数据库中的字符串
我的表:
create table MyTable (
Id int identity(1,1) not null,
MyStatus char(2) not null
)
insert into MyTable(MyStatus) select 'A'
类和枚举:
public class MyTable
{
public virtual int Id { get; set; }
public virtual MyTableStatus MyStatus { get; set; }
}
public enum MyTableStatus
{
A,
B
}
映射:
public MyTableMap()
{
Id(x => x.Id);
Map(x => x.MyStatus);
}
当我执行以下测试时,我得到 System.FormatException :输入字符串的格式不正确...
[Test]
public void Blah()
{
MyTable myTable = Session.Get<MyTable>(1);
Assert.That(myTable.MyStatus, Is.EqualTo(MyTableStatus.A));
}
将枚举映射到数据库中的字符串表示形式的正确方法是什么?
编辑 - 我正在现有数据库上编写我的应用程序,我无法轻松修改该数据库,因为它也被其他应用程序使用。因此,数据库中的某些字段(我想在应用程序中将其表示为枚举)的类型为 int ,某些字段的类型为 char(2) 。
My table:
create table MyTable (
Id int identity(1,1) not null,
MyStatus char(2) not null
)
insert into MyTable(MyStatus) select 'A'
Class and enum:
public class MyTable
{
public virtual int Id { get; set; }
public virtual MyTableStatus MyStatus { get; set; }
}
public enum MyTableStatus
{
A,
B
}
Mapping:
public MyTableMap()
{
Id(x => x.Id);
Map(x => x.MyStatus);
}
When I execute the following test, I get System.FormatException : Input string was not in a correct format...
[Test]
public void Blah()
{
MyTable myTable = Session.Get<MyTable>(1);
Assert.That(myTable.MyStatus, Is.EqualTo(MyTableStatus.A));
}
What is the right way to map an enum to it's string representation in the database?
Edit - I am writing my application on an existing database, which I cannot modify easily because it is used by other applications also. So some fields in the database (which I would like to represent as enums in my application) are of type int and some of type char(2).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建一个自定义 IUserType 将枚举与其字符串表示形式相互转换。有一个 这里是 C# 的好例子 和 VB.NET 中使用枚举的示例(向下滚动到实现 IUserType)。
You need to create a custom IUserType to convert an enum to its string representation and back. There's a good example in C# here and an example in VB.NET for working with enums here (scroll down to implementing IUserType).
据我所知,默认情况下,NHibernate 仅将枚举作为字符串存储在数据库中。我想我知道这里的问题是什么。您创建表的方式不正确。
如果您使用 Nhibernate,请使用它的构建配置函数来创建表,而不是手动创建表,然后您将看到您的枚举存储为字符串。
我们在应用程序中广泛使用枚举,将其作为字符串存储在数据库中是有意义的。原因很简单,如果我向枚举汤姆添加一个新值,然后如果未设置默认值,那么我的代码和我的数据紧密耦合,这是我绝对不想要的。
另外,您可以使用 varchar 作为属性,而不是使用 char 作为字符串。
更新后:
在将其存储到数据库之前,你们不能做一些操作吗?因此,当您想要存储新的 char 枚举时,编写一个为您生成 int 值的函数,并将其存储在属性中,然后保存它,或者如果您想让它变得简单,该函数可以有一个 switch case。
因此,您要做的就是不获取从数据库检索的此属性,而是在 Status 类中添加一个新属性,该属性基本上具有获取适当枚举的逻辑。
你认为这是个好主意吗?
希望这有帮助。
Well as far as I am aware NHibernate stores enums as string only in the db by default. I think I know what the problem here is. The way you are creating the table is incorrect.
if you are using Nhibernate use it build configuration function to create the tables instead of creating the tables manually and then you will see that your enum is stored as string.
We use enums extensively in our app and it makes sense for us to store it as strings in the db. The reasons are simple if I add a new value to an enum tom then if default values are not set then my code and my data are tightly coupled which I definitely wouldnt want.
Also instead of using char for your string can you use varchar for the property.
After the update:
Cant you guys do some kind of manipulation before you store it in the database? Thus when you want to store the new char enums write a function that generates an int value for you and store this in the propertry and now save it or if you want to make it simple the function can have a switch case.
So what you do is you dont have a get on this property that is retrieved from the db instead you add a new property in the class Status that basically has the logic of getting the appropriate enum.
Do you think thats a good idea?
Hope this helps.