简体   繁体   English

如何首先从数据库EF模型向现有代码添加其他表?

[英]How to add additional tables to an existing code first from database EF model?

When creating a new entity data model from an existing database using code first from database approach, one can directly specify which tables to include in the model. 当先使用数据库方法中的代码从现有数据库创建新的实体数据模型时,可以直接指定要在模型中包括哪些表。 A subset (or all) tables can be selected in the Visual Studio wizard: 可以在Visual Studio向导中选择一个子集(或所有)表:

EF数据模型向导

In this example, Category and Product classes will be created along with the DbContext derived context class. 在此示例中,将创建CategoryProduct类以及DbContext派生的上下文类。 If I later want to add additional tables to the same model, is there a simple way to add them, ie without having to manually create the classes myself? 如果以后我想将其他表添加到同一模型中,是否有一种简单的方法来添加它们,即不必自己手动创建类?

Eg initially, my DbContext class will contain: 例如,最初,我的DbContext类将包含:

public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Product> Products { get; set; }

Now suppose I also want to include Employee table so that the DbContext class will be updated to: 现在假设我还想包含Employee表,以便将DbContext类更新为:

public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<Employee> Employees { get; set; }

VS context menus don't seem to provide this option, but maybe I'm missing something. VS上下文菜单似乎没有提供此选项,但也许我缺少了一些东西。 Is there a way to bring back the wizard so I can select additional tables? 有没有办法恢复向导,以便我可以选择其他表?

One solution is to have a separate empty project where you simply create a new model and then copy/paste new classes, but I'm curious if there's a faster way. 一种解决方案是拥有一个单独的空项目,您可以在其中简单地创建一个新模型,然后复制/粘贴新类,但是我很好奇是否有更快的方法。

No, there is no way to do that. 不,没有办法做到这一点。 You must run the wizard again, and generate the desired classes. 您必须再次运行向导,并生成所需的类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM