我应该将数据库级别还是在应用程序逻辑(.NET)中存储? [英] Should I store my Enums at database level or in the application logic (.NET)?

查看:154
本文介绍了我应该将数据库级别还是在应用程序逻辑(.NET)中存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,让它调用对象。它有一组预定义的记录,如下所示:

  ObjectId ObjectName 

1 Salmon
2 Trout
3 Seabass
4 Shark

Etc ..



所以,我想以某种方式实现枚举。但是最好的办法是在数据库中创建一个表,或者在CommonEnums codebehind文件中的应用程序逻辑中实现它?

解决方案

p>我们在两个地方添加它们,我们使用代码生成器来保持值的一致。



主要值在枚举中,但模板会自动生成一个脚本更新DB



我们还向我们的实体添加一个属性,以公开一个枚举属性,而不是保存到DB的整数



编辑:添加一个如何做的例子(在VB.NET中:

 < EnumDbTableInfo(TableName,keyColumn,descColumn)> _ 
公开枚举CasoImportacion As Short

<描述()> _
正常= 0

<描述(第一个描述)> _
FirstRealValue = 1

<描述(第二个)&$ _
AnotherVa lue = 2

<描述(第三个描述)> _
LastValue = 3


结束枚举

稍后,EnumHelper类接受具有EnumDbTableInfo属性的类,并生成脚本以更新DB



最佳方案


I have a table, lets call it Objects. It has a predefined set of records looking like this:

ObjectId ObjectName

  1       Salmon
  2       Trout
  3       Seabass
  4       Shark

Etc..

So, this I would like to implement somehow as an enum. But what's the best way, implement it in the database creating tables for it or in the application logic in an CommonEnums codebehind file etc?

解决方案

We add them in both places, we use code generator to keep in sync the values.

The main values are in the Enum but the template automatically generates a script to update the DB

We also add a property to our entities to expose an enum property instead of the integer that is saved to the DB

Edit: Adding an example of how to do it (In VB.NET:

    <EnumDbTableInfo("TableName", "keyColumn", "descColumn")> _
    Public Enum CasoImportacion As Short

        <Description("")> _
        Normal = 0

        <Description("The First Description")> _
        FirstRealValue = 1

        <Description("A second one")> _
        AnotherValue = 2

        <Description("Third Description")> _
        LastValue = 3


End Enum

Later an EnumHelper class take the classes that has the attribute EnumDbTableInfo and generates the script to update the DB

Best Regards

这篇关于我应该将数据库级别还是在应用程序逻辑(.NET)中存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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