如何指定要由DbContext使用的表的名称

编程入门 行业动态 更新时间:2024-10-25 04:26:25
本文介绍了如何指定要由DbContext使用的表的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我之前的问题的后续问题。我的印象是,如果数据库中有多个表,则将使用 DbSet 变量的名称来标识表。

This is a followback question from my earlier question. I was under the impression that if there're more than one table in the database, the name of the DbSet variable will be used to identify the table.

然而,在链接的问题中,很明显, DbContext 正在选择 code>表,而不是投资组合表,即使变量的名称是投资组合。我仍然可以将变量名改为任何东西,而且我仍然收到数据。

However in the linked question it was clear that the DbContext was choosing Products table instead of Portfolio table even if the name of the variable was Portfolio. I can still change the variable name to anything and I am still getting data.

所以我的问题是如何通过 DbContext 映射表?我需要在项目中添加更多的表,并且不知道如何使用单个 DbContext 对象(或者应该使用单独的 DbContext 对于同一数据库中的单独表)?

So my question is how the tables are mapped by DbContext? I need to add few more tables in the project and have no idea how would I do that using a single DbContext object (or should I use separate DbContext for separate Tables in the same database)?

推荐答案

您可以在实体上执行以下操作:

You can do the following on the entity:

[Table("Portfolio")] public class Product { /* ... */ }

按照惯例,该表以多个实体名称命名。 所以 DbSet< Product> 将默认存储/查找名为 Products 的表。

By convention the table is named after the plural of the entities name. So a DbSet<Product> will by default be stored / looked up in a table named Products.

更多推荐

如何指定要由DbContext使用的表的名称

本文发布于:2023-11-05 09:31:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要由   名称   DbContext

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!