简体   繁体   English

如何从统计类型数据的数据库即时获取枚举中的数据? 在asp.net核心mvc中

[英]How to get data in enum from database instant of stastically typed data? in asp.net core mvc

Dept.cs部门

public enum Dept
{
    None,
    HR,
    IT,
    Payroll
}

above Dept enum data is already typed but want fetch data from database in this enum using asp.net core mvc上面的部门枚举数据已经输入,但想要使用asp.net core mvc 从这个枚举中的数据库中获取数据

Model Department.cs模型部.cs

public class Department
{
    public int DeptId { get; set; }
    public string DeptName { get; set; }
} 

You can't change the value of an enum member.您不能更改enum成员的值。 Enums are, by definition, groups of constants - which means that the values of their members is set at compile time and can't be changed.根据定义,枚举是一组常量——这意味着它们的成员的值是在编译时设置的,不能更改。

From the Enumeration types (C# reference) page on Microsoft Docs:从 Microsoft Docs 上的枚举类型(C# 参考)页面:

An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type.枚举类型(或枚举类型)是由一组底层整数类型的命名常量定义的值类型。

This is why using Database data as enums is a bad idea - once the database data has changed, you'll need to change your code as well, recompile and redistribute it.这就是为什么使用数据库数据作为枚举是一个坏主意 - 一旦数据库数据发生变化,您也需要更改代码,重新编译并重新分发它。

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

相关问题 如何从 ASP.NET Core MVC 中的数据库中获取相关数据 - How to get related data from database in ASP.NET Core MVC 使用ASP.NET MVC Core将数据从数据库注入到类中 - Inject data from database to a class with ASP.NET MVC Core 我如何引用MVC项目中的类,该类使用asp.net核心从数据库中检索数据 - How can I reference a class in MVC project that retrieves data from database with asp.net core 如何将数据从复选框(html)导入数据库 ASP.NET Core MVC - How to import data from checkbox (html) to database ASP.NET Core MVC 如何使用ASP.net MVC从数据库中获取数据? - How to fetch data from database using ASP.net MVC? ASP.net Core:如何从Service获取文件数据? - ASP.net Core: How to get file data from Service? 如何将数据库中的数据添加到 ASP.NET Core MVC (.NET 6) 应用程序中的共享 _Layout.cshtml? - How do you add data from the database to the shared _Layout.cshtml in ASP.NET Core MVC (.NET 6) application? 如何从 API 获取数据并将其插入数据库而不在 ASP.NET MVC 中重复? - How to get data from API and insert it into database without repeating in ASP.NET MVC? 如何在ASP.net MVC中获取数据 - How to get data in ASP.net MVC 如何在同一项目中的ASP.NET MVC控制器中从ASP.NET Web API获取数据 - How get data from asp.net web api in asp.net mvc controller in the same project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM