简体   繁体   English

使用实体框架管理TypeList

[英]Managing TypeLists w/ Entity Framework

I am in a predicament where I have several lists as part of the business logic that will need to be dropdown selections (w/ Id as value and FriendlyName as Description ) on the front-end. 我处于困境中,在业务逻辑中我有几个列表,这些列表需要在前端进行下拉选择(w / Id为value, FriendlyNameDescription )。

Additionally, I will be doing different things in the business logic based on the saved database selection. 另外,根据保存的数据库选择,我将在业务逻辑中做不同的事情。 This means I will either need GlobalConstants or Enums that match the Ids in my code. 这意味着我将需要与代码中的ID匹配的GlobalConstantsEnums

Should I use Enums , Create a table for every single list, or create a general tables called TypeList and TypeListItem when using EF? 我应该使用Enums ,为每个列表创建一个表TypeListItem在使用EF时创建一个称为TypeList和TypeListItem的常规表?

This answer does not provide a concrete solution to your problem, but I hope it can give you some hint. 该答案不能为您的问题提供具体的解决方案,但希望它能给您一些提示。

There are several posts in SO about that issue, for instance this one . SO中有很多关于该问题的帖子,例如this A popular way to do it is to have enums in your code, and also tables in your database to store the enum extended info (like display names and other data), and to keep them synchronized by applying changes in your enums to the database tables contents using custom code (try to avoid having to manually write SQL scripts). 一种流行的方法是在代码中包含枚举,并在数据库中包含表以存储枚举扩展信息(如显示名称和其他数据),并通过将枚举中的更改应用于数据库表来使其保持同步内容使用自定义代码(尽量避免必须手动编写SQL脚本)。

EF6 does not provide an out-of-the-box solution for this. EF6并未为此提供现成的解决方案。 There are several Nuget packages that implement that synchronization custom code. 有多个Nuget包可实现该同步自定义代码。 I can't give you advice about them as I have not used them. 由于我没有使用过它们,因此我无法为您提供建议。

Also you have to consider your specific scenario: how frequently your lists change, the size of your lists, whether you only need some pretty name to display or some other extra data, whether you will allow your users to change the lists or it will only be done by the developers, whether you need these data to be changed dynamically at run time (without modifying/compiling/publishing your code), the version of EF you are using, etc. A future version of EF might provide support for this. 另外,您还必须考虑自己的特定情况:列表更改的频率,列表的大小,是否只需要显示漂亮名称或其他一些数据,是否允许用户更改列表或仅由开发人员完成,是否需要在运行时动态更改这些数据(无需修改/编译/发布代码),所使用的EF版本等。EF的未来版本可能对此提供支持。

If your case if you only have this FriendlyName extended data and none of the complex scenarios cited above, perhaps you could manage without the database tables. 如果您的情况是仅拥有这个FriendlyName扩展数据并且没有上面引用的任何复杂方案,那么也许可以在没有数据库表的情况下进行管理。 You could for instance use an enum for each list, and an extension where you define an array or dictionary for your enum where you store those extra data. 例如,您可以为每个列表使用一个枚举,并为在其中存储这些额外数据的枚举定义一个数组或字典来使用扩展名。 You would have to update those data in your code every time the list of values changes. 每当值列表更改时,您都必须在代码中更新这些数据。

An example implementation of this for a simple enum CategoryStatus with two values and and extra LocalizedName item (which allows to use Resources values for translated texts): 一个简单的枚举CategoryStatus一个示例实现,该枚举CategoryStatus具有两个值和一个额外的LocalizedName项目(允许将Resources值用于翻译后的文本):

public enum CategoryStatus
{
    Active = 0,
    Inactive = 1
}

public static class CategoryStatusExtension
{
    private static string[] localizedNames = new string[] { 
        Resources.Strings.CategoryStatus_Active,
        Resources.Strings.CategoryStatus_Inactive
    };

    public static string LocalizedName(this CategoryStatus self)
    {
        switch (self)
        {
            case CategoryStatus.Active:
                return localizedNames[0];
                break;

            case CategoryStatus.Inactive:
                return localizedNames[1];
                break;
        }
        return null;
        //int index = (int)self - 1;
        //return localizedNames[index];
    }
}

You use it like this: 您可以这样使用它:

var categoryStatus = CategoryStatus.Active;
var textToUse = categoryStatus.LocalizedName();

You could also find a way to automate a little more the search of the values instead of using an explicit switch , like in the commented out code. 您还可以找到一种方法来自动化更多的值搜索,而不是使用显式switch (例如在注释掉的代码中)。

About using only one table for all enums, it again depends on those kind of considerations. 关于仅对所有枚举使用一个表,它再次取决于这些考虑因素。 If your lists are small, you don't have many of them, and you will not get a lot of new lists in the future, then you should probably use separate tables for each enum. 如果您的列表很小,那么您将没有很多,并且将来不会有很多新列表,那么您应该为每个枚举使用单独的表。 I think your code will be easier to write/maintain if you use separate tables. 我认为如果使用单独的表,您的代码将更易于编写/维护。

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

相关问题 实体框架5 POCO-创建和管理关系 - Entity Framework 5 POCOs - creating and managing relationships 具有一对多和一对一的实体框架实体? - Entity Framework Entity w/ One-to-Many and One-to-One? 在项目中管理Entity Framework的程序包管理器控制台命令 - Managing Package Manager Console commands for Entity Framework in project 在Entity Framework核心中更新和管理多对多关系 - Updating and managing many-to-many relation in Entity Framework core 实体框架LINQ-访问实体字段而不获取它 - Entity framework LINQ - access entity field w/o fetching it 使用Entity Framework Core随时间管理数据库标准化 - Managing normalising a database over time using Entity Framework Core 在带有实体框架的新MVC 3应用中使用ProfileProvider是一个坏主意吗? - Is it a bad idea to use ProfileProvider in new MVC 3 app w/Entity Framework? 带实体框架的MVC从下拉列表重定向到编辑页面 - MVC w/ Entity Framework redirecting to edit page from dropdownlist 带有身份的ASP.NET:实体框架在SaveChanges上引发System.Data.Entity.Core.UpdateException - ASP.NET w/ Identity: Entity Framework throws System.Data.Entity.Core.UpdateException on SaveChanges 一对多和多对多关系实体框架,包括默认授权和角色管理 - One to Many and a Many to Many relationship Entity Framework including default Authorization and role managing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM