如何指定表格的名称被的DbContext使用 [英] How to specify the name of the table to be used by DbContext

查看:582
本文介绍了如何指定表格的名称被的DbContext使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从一个前面的问题。我是IM pression下,如果有数据库是多个表中, 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 就是选择产品表,而不是组合表,即使变量的名字是投资。我仍然可以改变变量名到任何东西,我仍然得到的数据。

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)?

推荐答案

您可以对实体如下:

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

按照惯例该表的实体名称的复数而得名。结果
因此,一个 DbSet<产品方式> 将被默认保存/在一个名为表中查找产品

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使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆