SQL表和C#枚举 [英] SQL Table and C# Enumeration

查看:157
本文介绍了SQL表和C#枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有 N 的类型在我的应用程序的用户。

Suppose I have n types of users in my application.

我使用的是用户类型枚举来区分它们。

I am using an UserType enumeration to distinguish them.

我需要保持一个表,我叫用户类型?

Do I need to keep a table in my database named UserType?

所以,我可以找到用户通过查询表,而不是搜索应用程序源代码键入任意时间。

So that I can find the user type any time by querying the tables rather than searching the application source code.

这样做我的源代码可能会变得有些复杂。

Doing so my source code may become somewhat complicated.

我应该承认这个权衡?

推荐答案

有关理解数据结构,我们创建一个自定义类型的查找表,即使他们永远不会改变的缘故。这样你保持参照完整性,也通过关联表来此查找。

Yes, use both: UserType lookup table and enumeration

For the sake of understanding data structures we do create lookup tables with defined types even though they never change. This way you keep referential integrity by also relating tables to this lookup.

自动化您枚举结果
通过的使用T4模板您可以轻松地自动化您的业务层代码来反映DB变化。所以每当你改变你的SQL脚本来改变你的查找表,你只要运行你的模板,并在枚举创建额外的价值。和BTW:所有T4模板可以在Visual Studio 2008中,只需一次点击执行选择在解决方案资源管理解决方案,并单击Solution Explorer中的小工具栏中的图标。瞧。 T4S所有生成的。

Automate your enums
By using T4 templates you can easily automate your business layer code to reflect DB changes. So whenever you change your SQL script to change values in your lookup table, you just run your templates and create additional values in your enums. And BTW: all T4 templates can be executed with a single click in Visual Studio 2008. Select your solution in Solution Explorer and click an icon in the mini toolbar of Solution Explorer. Voila. T4s all generated.

标志枚举结果
他们都好得很,但在T-SQL脚本复杂化情况下,你也将使用他们在同样的方式在业务层。也许是更加睿智使用多对多的关系,而不是,但你将无法自动创建枚举,所以使得DB层上的变化也将意味着使业务层上的变化。

Flag enums
They are all fine and dandy, but it complicates T-SQL scripts in case you would also use them in the same way as in your business layer. Maybe it is more wise to use many-many relationship instead, but you won't be able to automate enum creation, so making a change on DB layer would also mean making a change on business layer.

这篇关于SQL表和C#枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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