具有实体框架的枚举 - 枚举值独立于表行 id

发布于 2024-11-03 03:48:26 字数 590 浏览 1 评论 0原文

我有客户表:客户(id,名称 status_id)。
我有状态表:状态(id,名称,代码)。
客户和状态之间存在关系(FK):status_id(客户)= id(状态)。

在.NET代码中,我有 Status 枚举和 Customer 类:

enum Status {
    status_1,
    status_2,
    status_3,
    status_4
}

class Customer {
    public virtual long id {get;set;}
    public virtual string name {get;set;}
    public virtual Status customer_status {get;set;}
}

枚举没有数据库中状态行的值,因为我不想在代码中创建数据的依赖关系(硬编码)。

如何使用实体框架在 edmx 中呈现客户类?

编辑:
我发现的所有解决方案都假设枚举值与 Customer 表中的 status_id 相同。他们甚至没有状态表,这对我来说非常重要,以便限制 status_id 的值可能性

I have Customers table: Customer(id, name status_id).
I have Statuses table: Status(id, name, code).
There is relation (FK) between Customer and Status: status_id (Customer) = id (Status).

In the .NET code I have Status enum and Customer class:

enum Status {
    status_1,
    status_2,
    status_3,
    status_4
}

class Customer {
    public virtual long id {get;set;}
    public virtual string name {get;set;}
    public virtual Status customer_status {get;set;}
}

The enums doesn't have the values of the statuses rows from the db because I don't want to create dependency of the data in the code (hard coded).

How can I use the entity framework in order to present the customer class in the edmx?

EDIT:
All the solutions I found assumes that the enum values are the same as the status_id in Customer table. They even doesn't have the Status table which is very importent to me in order to make constraint on the values possibility of status_id

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2024-11-10 03:48:26

决不。根本不支持枚举,因此您必须更改类以包含 statusId(不必是公共的)和不映射的 customer_status,后者将处理数据库记录到枚举的转换。您必须包含对记录的依赖关系,因为您的代码必须知道哪个记录 Id 代表哪个枚举成员。这不是结构映射,而是数据映射,当然包括数据依赖。

也不要将 Statuses 表包含到您的模型中。

有一个 在 EFv4 中伪造枚举的方法,但它需要更多地更改代码,并且枚举成员可能必须使用与数据库记录相同的值。

No way. Enums are not supported at all so you must change your class to include both statusId (doesn't have to be public) and not mapped customer_status which will handle translation of database record to the enum. You must include the dependency to a recrod because your code must know which record Id represents which enum member. That is not structure mapping but data mapping which of course include data dependency.

Also do not include Statuses table to your model.

There is one approach to fake enums in EFv4 but it would require even more changing your code and enum members would probably have to use same values as database records.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文