简体   繁体   English

ASP.NET MVC 3代码优先使用空数据库

[英]ASP.NET MVC 3 Code First with an Empty Database

I am new to ASP.NET MVC but have worked with RoR quite a bite so I am familiar with the MVC pattern. 我是ASP.NET MVC的新手,但是与RoR一起工作了很多,所以我熟悉MVC模式。

I am curious about the possibilities when handling the database part when using the Code First approach. 我对使用“代码优先”方法处理数据库部件时的可能性感到好奇。 I was told by an ASP.NET developer colleague that you can create an empty database, that is a database with no tables and use EF Code First to create the tables in that empty database. 一个ASP.NET开发人员同事告诉我,您可以创建一个空数据库,即没有表的数据库,并使用EF Code First在该空数据库中创建表。

Is that correct? 那是对的吗?

I cannot find any tutorials/documentation on that particular approach anywhere. 我在任何地方都找不到关于该特定方法的任何教程/文档。 I have come across Code First approaches that start with an existing database and tables but not an existing database without tables. 我遇到了Code First方法,该方法从现有数据库表开始,而不是没有表的现有数据库。

I would love to know: 我会很高兴知道:

If this is possible. 如果可能的话。 If it isn't, what's the next-closest approach? 如果不是,那么下一个最接近的方法是什么?

Thanks 谢谢

Thanks 谢谢

Yes, it is possible by design. 是的,可以通过设计实现。 Here is the official tutorial 这是官方教程

http://msdn.microsoft.com/en-us/data/jj591621.aspx http://msdn.microsoft.com/en-us/data/jj591621.aspx

See section "Building an Initial Model & Database" 请参阅“建立初始模型和数据库”部分

Note: If you database already exists, you can either name your dbcontext class as that database, or specify "Initial Catalog" in connection string to match your database. 注意:如果数据库已经存在,则可以将dbcontext类命名为该数据库,或者在连接字符串中指定“初始目录”以匹配数据库。

At it's most basic level, you create a class to represent your table, a context class (derived from DbContext) and then execute commands in the Package Manager Console to create migrations/scripts and/or update the database schema directly (think of it as rake db migrate for EF Code First). 在最基本的级别上,您将创建一个代表表的类,一个上下文类(从DbContext派生),然后在Package Manager控制台中执行命令以创建迁移/脚本和/或直接更新数据库架构(将其视为rake db migrate为EF代码优先)。

public class User
{
    public string Username {get;set;}
    public string Password {get;set;}
}

public class DatabaseContext : DbContext
{
    public DbSet<User> Users {get;set;}
}

Tools -> Library Package Manager -> Package Manager Console 工具 -> 库软件包管理器 -> 软件包管理器控制台

Once the console window is open, make sure the project with your entity/context is selected in the "Default Project" dropdown at the top of the window and execute the following commands: 打开控制台窗口后,确保在窗口顶部的“默认项目”下拉菜单中选择了具有您的实体/上下文的项目,并执行以下命令:

Add-Migration "Description of your migration"

This will create a migration class for you that represents your changes to the database since the last migration. 这将为您创建一个迁移类,代表您自上次迁移以来对数据库所做的更改。 If this is the first one (given that you're starting from an empty database), it will also generate a system table called "__MigrationHistory" which is used to track which migrations have already been applied to the database. 如果这是第一个(假设您是从一个空的数据库开始的),它还将生成一个名为“ __MigrationHistory”的系统表,该表用于跟踪已将哪些迁移应用于数据库。

Update-Database -script

Executing Update-Database with the -script flag will generate a SQL script you can save/check in to source control if needed. 使用-script标志执行Update-Database将会生成一个SQL脚本,您可以在需要时将其保存/签入源代码管理。 If you'd prefer just to update the database itself without the script, just leave off the -script tag. 如果您只想在不使用脚本的情况下更新数据库本身,则不要使用-script标记。

Be sure you have a connection string in web.config named the same thing as your context class ("DatabaseContext" in this example). 确保在web.config中有一个连接字符串,其名称与上下文类相同(在此示例中为“ DatabaseContext”)。 This can point to your existing database but it will only manage/track changes to entities which are defined in your context class. 这可以指向您现有的数据库,但是它将仅管理/跟踪对上下文类中定义的实体的更改。 I've found sometimes it's necessary to explicitly define the context when using the Add-Migration and Update-Database commands like so: 我发现有时候在使用Add-Migration和Update-Database命令时,有必要显式定义上下文,如下所示:

Add-Migration "Description of your migration" -connectionstringname "DatabaseContext"

You can simply create a class wich that is your table like 您可以简单地创建一个与表类似的类

 public class Person{
public Guid id {get;set;};
public string Name {get;set;};
}

then you can add a ADO.NET data entity model type codefirst empty database then add the class into that database 然后您可以添加ADO.NET数据实体模型类型代码,首先是空数据库,然后将类添加到该数据库中

add a ctor in db 在数据库中添加一个ctor

static myDB(){
Database.setInitializer(new DropCreateDataBaseIfModelChanges<myDB>());
}


public virtual dbset<Person> People {get;set;};

after that your database will be create 之后,您的数据库将被创建

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

相关问题 ASP.NET MVC 代码优先构建空迁移 - ASP.NET MVC Code-First Building Empty Migrations 空迁移首先在代码中生成ASP.NET MVC - Empty Migrations are generated in code first ASP.NET MVC asp.net mvc3代码优先(数据库单例) - asp.net mvc3 Code First (Database Singleton) ASP.net MVC:使用具有代码优先和现有数据库的 EF6 创建 MVC 5 应用程序 - ASP.net MVC : Create MVC 5 Application using EF6 with Code First and Existing Database asp.net mvc 5 DropDownListFor始终为空 - asp.net mvc 5 DropDownListFor with empty first item is always Required 如何使用首先通过代码创建的数据库中的数据填充谷歌图表 - ASP.Net MVC - How to populate google charts using data from database created via code first - ASP.Net MVC 代码首先不要将类的Dictionary属性放入数据库,ASP.NET MVC4 - Code first not putting Dictionary property of class into database, ASP.NET MVC4 使用EF6将文档存储在数据库ASP.NET MVC5中-代码优先 - Storing documents in database ASP.NET MVC5 with EF6 - code-first 如何使用“代码优先实体框架”在ASP.NET MVC 4中创建数据库并将其连接到数据库? - How to create a database and connect to it in ASP.NET MVC 4 by using Code First Entity Framework? ASP.NET MVC 3 EF Code First - 也使用现有数据库 - ASP.NET MVC 3 EF Code First - also use existing database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM