实体框架代码首先自动创建表

时间:2022-11-07 09:28:28

I'm trying to understand what in the world Entity Framework is doing under the hood that causes the observed behavior I am about to describe.

我试着去理解世界实体框架下发生了什么导致了我将要描述的观察到的行为。

This is an ASP.NET MVC 4 Internet Application project.

这是一个ASP。NET MVC 4 Internet应用程序项目。

I have an existing MSSQL database on localhost called wpdb.

我在本地主机上有一个名为wpdb的现有MSSQL数据库。

Web.config:

. config:

<add name="DefaultConnection"
    connectionString="Data Source=localhost;Initial Catalog=wpdb;User=xxx;Password=xxx"
    providerName="System.Data.SqlClient" />

When I first run the web app, it creates dbo.UserProfile, dbo.webpages_Membership and a few others tables auto-magically.

当我第一次运行web应用程序时,它创建了dbo。UserProfile dbo。webpages_Membership和其他一些表会自动魔术般地显示出来。

I create a new model Comments.cs

我创建了一个新的模型注释

public class Comment
{
    public int ID { get; set; }
    public string Text { get; set; }
}

public class CommentDBContext : DbContext
{
    public DbSet<Comment> Comments { get; set; }
}

I add a new controller CommentsController as MVC controller with read/write actions and views, using Entity Framework (and select my "Comment" and "CommentDBContext" classes).

我使用实体框架添加了一个新的控制器CommentsController作为MVC控制器,具有读/写操作和视图(并选择“Comment”和“CommentDBContext”类)。

Then I add a new connection string (apparently Entity Framework's default connection string isn't "DefaultConnection" but something that ends up being invalid, I'm not sure why).

然后我添加了一个新的连接字符串(显然实体框架的默认连接字符串不是“DefaultConnection”,而是一些最终无效的东西,我不确定为什么)。

<add name="CommentDbContext"
    connectionString="Data Source=localhost;Initial Catalog=wpdb;User=xxx;Password=xxx"
    providerName="System.Data.SqlClient" />

Now when I browse to localhost/Comments Entity Framework auto-magically creates a new table in my database called dbo.Comments.

现在,当我浏览到localhost/Comments实体框架时,会神奇地在我的数据库中创建一个名为dbo.Comments的新表。

Everything I've described so far is exactly as expected, here's where it's gets weird

到目前为止,我所描述的一切都与预期完全一致,这就是它变得奇怪的地方

I accidentally deleted dbo.Comments. However, this time when I browse to localhost/Comments Entity Framework won't recreate the table. Instead it throws an "Invalid object 'dbo.Comments'" error at me.

我不小心删除dbo.Comments。但是,当我浏览到localhost/Comments实体框架时,不会重新创建表。相反,它抛出一个“无效对象”dbo。评论”错误在我。

Why does it do this? Why doesn't it just recreate the database? I even tried deleting all of my Model / Controller / View code, cleaning / rebuilding the solution, and then recreated it all again, and EF still refuses to recreate the table!

为什么会这样呢?为什么不重新创建数据库呢?我甚至尝试删除我所有的模型/控制器/视图代码,清理/重建解决方案,然后重新创建它,EF仍然拒绝重新创建表!

What's even more confusing, if I comment out the CommentDbContext line in my Web.config file (so that the EF code is trying to connect to the wrong database) then run the application and browse to /Comments, then kill the app, uncomment the connection string, this time it creates a brand new dbo.Comments table without any problems!

更让人困惑的是,如果我在我的Web上注释掉了CommentDbContext。配置文件(使EF代码试图连接到错误的数据库)然后运行应用程序并浏览到/Comments,然后杀死应用程序,取消对连接字符串的注释,这一次它创建了一个全新的dbo。注释表没有任何问题!

Why does changing then connection string to something invalid then changing it back allow EF to generate table from my models again? There seems to be some state being preserved somewhere it shouldn't be..

为什么将连接字符串更改为无效,然后将其更改为允许EF从我的模型中再次生成表?似乎有某种状态被保存在不该存在的地方。

1 个解决方案

#1


2  

I accidentally deleted dbo.Comments. it throws an "Invalid object 'dbo.Comments'" error at me. Why does it do this?

我不小心删除dbo.Comments。它抛出一个“无效对象”dbo。评论”错误在我。为什么会这样呢?

Because you aren't suppose to edit/delete the table the way you've configured it. It is not syncronizing database changes to models and models to the database auto-magically.

因为您不需要按照您配置的方式来编辑/删除表。它不会自动地同步数据库对模型和模型的更改。

Why doesn't it just recreate the database?

为什么不重新创建数据库呢?

Because you haven't told it that automatically, nor have you asked it to.

因为你没有自动地告诉它,也没有要求它。

Why does changing then connection string to something invalid then changing it back allow EF to generate table from my models again?

为什么将连接字符串更改为无效,然后将其更改为允许EF从我的模型中再次生成表?

Because it creates a table (forgot the name) that knows about the last time it synchronized and what tables it has and hasn't created. When you change the connection string to a different database that does not have the table, it recreates everything.

因为它创建了一个表(忘记了名称),它知道它上一次同步的时间,以及它拥有和没有创建的表。当您将连接字符串更改为没有表的另一个数据库时,它将重新创建所有内容。

You should probably take a look at the following articles on MSDN and choose how you want Entity Framework to work for your scenario:

您可能应该看看下面关于MSDN的文章,并选择您希望实体框架如何为您的场景工作:

Code First Migrations

代码首先迁移

#1


2  

I accidentally deleted dbo.Comments. it throws an "Invalid object 'dbo.Comments'" error at me. Why does it do this?

我不小心删除dbo.Comments。它抛出一个“无效对象”dbo。评论”错误在我。为什么会这样呢?

Because you aren't suppose to edit/delete the table the way you've configured it. It is not syncronizing database changes to models and models to the database auto-magically.

因为您不需要按照您配置的方式来编辑/删除表。它不会自动地同步数据库对模型和模型的更改。

Why doesn't it just recreate the database?

为什么不重新创建数据库呢?

Because you haven't told it that automatically, nor have you asked it to.

因为你没有自动地告诉它,也没有要求它。

Why does changing then connection string to something invalid then changing it back allow EF to generate table from my models again?

为什么将连接字符串更改为无效,然后将其更改为允许EF从我的模型中再次生成表?

Because it creates a table (forgot the name) that knows about the last time it synchronized and what tables it has and hasn't created. When you change the connection string to a different database that does not have the table, it recreates everything.

因为它创建了一个表(忘记了名称),它知道它上一次同步的时间,以及它拥有和没有创建的表。当您将连接字符串更改为没有表的另一个数据库时,它将重新创建所有内容。

You should probably take a look at the following articles on MSDN and choose how you want Entity Framework to work for your scenario:

您可能应该看看下面关于MSDN的文章,并选择您希望实体框架如何为您的场景工作:

Code First Migrations

代码首先迁移