使用 Dapper 和具有复合键的数据库表生成动态 C# 类

发布于 2025-01-19 20:42:25 字数 356 浏览 1 评论 0原文

我有一个具有组合键的数据库表。在我的 C# 代码中,我不使用域类,但根据数据库中配置的表名称,我读取该名称并生成 C# 域类。有没有人尝试过从具有组合键的配置表动态生成 C# 类?

例子: 假设我有一个 User 表,其中包含组合键、UserIDUserName。我使用反射和 Dapper 在 C# 中动态生成 User 类。如何动态生成 User 类并将记录插入到 User 表中,该表现在需要 2 个值,UserIDUserName< /代码>?

I have a database table that has a composite key. In my C# code, I don't use Domain classes but based on the name of the table configured in the database, I read that and generate the C# domain class. Has anyone tried to dynamically generate a C# class from the configuration table that has a composite key?

Example:
Let's say I have a User table with a composite key, UserID and UserName. I dynamically generate the User class in C# using reflection and Dapper. How can I generate the User class dynamically and insert a record into the User table which now requires 2 values, UserID and UserName?

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

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

发布评论

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

评论(1

最终幸福 2025-01-26 20:42:25

对于带有复合密钥的模型,我使用了属性[explicitkey]而不是[key]。

In the case of models with composite keys, I have used the attribute [ExplicitKey] instead of [Key].

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