简体   繁体   English

使用Entity Framework Code First开发,我能否在现有数据库中创建一个或多个表?

[英]Using Entity Framework Code First Development, can I get it to create a table or tables in an existing database?

I'm new to Entity Framework, and I have an existing database that I'd like to add a few tables to, rather than creating a database from scratch for my new tables. 我是Entity Framework的新手,我有一个现有数据库想要添加一些表,而不是从头开始为新表创建数据库。 Is this possible with Entity Framework? 使用实体框架可以做到吗?

I've looked through the following MSDN articles on Code First development, and haven't been able to find the answer to my issue: 我浏览了有关Code First开发的以下MSDN文章,但找不到我的问题的答案:

Code First Development 代码优先开发

Code First Conventions 代码优先约定

Code First Fluent API 代码优先Fluent API

Code First Data Annotations 代码优先数据注释

I have had the same experience with my asp.net mvc3 project, it's a little different from asp.net but I guess entity framework is the focus here. 我在asp.net mvc3项目中有相同的经验,它与asp.net略有不同,但是我想这里重点关注实体框架。

  1. create new table in sql server 在SQL Server中创建新表

  2. create corresponding model in your model project 在模型项目中创建相应的模型

  3. Add the new table object property in DbNameEntities.cs 在DbNameEntities.cs中添加新的表对象属性

    public DbSet NewTables { get; 公共DbSet NewTables {get; set; 组; } }

  4. In Application_Start() method, comment out the original code first approach 在Application_Start()方法中,注释掉原始代码优先方法

    System.Data.Entity.Database.SetInitializer(new SeedSampleData()); System.Data.Entity.Database.SetInitializer(new SeedSampleData());

replace with this: 替换为:

System.Data.Entity.Database.SetInitializer<DbNameEntities>(null);

Yes, this is certainly possible. 是的,这肯定是可能的。

A decent walk-through is available on Scott Gu's blog Scott Gu的博客提供不错的演练

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

相关问题 如何使用Entity Framework 6在运行时创建数据库和表? - How can I create a database and tables at runtime using Entity Framework 6? 如何在现有数据库中添加表而不会丢失实体框架代码优先5.0中的数据? - How can i add a table in my existing database without losing data in Entity-framework Code first 5.0 ? Code First Entity Framework不使用表创建数据库 - Code First Entity Framework does not create database with tables 实体框架代码优先使用现有数据库时的好处 - Entity Framework Code-First Benefits when using existing database 在现有MySQL数据库上使用实体框架4.1 Code-First - Using entity framework 4.1 Code-First on existing MySQL database 具有现有数据库视图的实体框架代码优先 - Entity Framework Code First with an existing database view Entity Framework 5代码优先于现有Informix数据库 - Entity Framework 5 Code First to Existing Informix Database 首先围绕现有数据库的实体框架代码 - Entity Framework code first around existing database 实体框架优先代码和现有数据库 - Entity Framework code-first and existing database 无法使用Entity Framework代码创建数据库 - 首先+没有数据库 - Can't create database with Entity Framework code-first + no database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM