简体   繁体   English

从现有数据库添加表到DbContext?

[英]add table to DbContext from existing database?

Using the wizard, I created a DbContext from an existing database and only selected one table to be pulled in. However now I need to add additional tables from that database to the Entity Model. 使用该向导,我从现有数据库中创建了一个DbContext,并且只选择了一个要插入的表。但是,现在我需要将该数据库中的其他表添加到实体模型中。

What is the easiest way to add more tables from an existing database to a context that I have already created and to also generate the respective models for those tables? 将现有数据库中的更多表添加到我已经创建的上下文中并为这些表生成相应模型的最简单方法是什么?

Easiest way is: 最简单的方法是:

1) Open your .edmx database 1)打开您的.edmx数据库

2) Right click and choose "Update model from database..." 2)右键单击并选择“从数据库更新模型...”

3) check the tables you want and click finish 3)检查所需的表,然后单击完成

4) EF will create all your entity classes for you 4)EF将为您创建所有实体类

If you want to add the tables manually to your DbContext, you can do so by adding properties of type DbSet to the DbContext. 如果要将表手动添加到DbContext,可以通过将类型DbSet的属性添加到DbContext来进行。 For ex: 例如:

public DbSet<CarColor> CarColors { get; set; }

I think EntityFramework Reverse Poco Generator is what you're searching for. 我认为您正在寻找EntityFramework反向Poco生成器 It's an extension for VS which allows to populate a TT file, then creating POCO entities for you. 这是VS的扩展,它允许填充TT文件,然后为您创建POCO实体。

It is downloadable here: EntityFramework Reverse Poco Generator 可在此处下载: EntityFramework反向Poco生成器

I've wrote a very brief guide you could refer to, in order to setupt the extension and use it for your means. 我已经写了一个非常简短的指南,您可以参考它,以设置扩展并将其用于您的目的。 Check it here: Introduction to EntityFramework Reverse POCO Generator for Visual Studio 在此处检查: 用于Visual Studio的EntityFramework反向POCO生成器简介

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

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