在Entity Framework中更新数据库模式

时间:2021-05-14 02:08:41

I installed VS SP1 and played around with Entity Framework.

我安装了VS SP1并使用了Entity Framework。

I created a schema from an existing database and tried some basic operations.

我从现有数据库创建了一个模式,并尝试了一些基本操作。

Most of it went well, except the database schema update.

除数据库架构更新外,大部分都进​​展顺利。

I changed the database in every basic way:

我以各种基本方式更改了数据库:

  • added a new table
  • 添加了一张新表

  • deleted a table
  • 删除了一张表

  • added a new column to an existing table
  • 在现有表中添加了一个新列

  • deleted a column from an existing table
  • 从现有表中删除了一列

  • changed the type of an existing column
  • 更改了现有列的类型

The first three went well, but the type change and the column deletion did not followed the database changes.

前三个进展顺利,但类型更改和列删除没有遵循数据库更改。

Is there any way to make is work from the designer? Or is it not supported at the moment? I didn't find any related material yet, but still searching.

有没有办法让设计师工作?或者目前不支持?我还没有找到任何相关材料,但仍在寻找。

7 个解决方案

#1


6  

I would guess that possibly those don't happen because they would break the build for existing code, but that's just a guess on my part.

我猜这可能不会发生,因为它们会打破现有代码的构建,但这只是我的猜测。

Here's my logic:

这是我的逻辑:

First, EF is supposed to be more than 1:1 table mapping, so it's quite possible that just because you are deleting a column from table A doesn't mean that for that entity, there shouldn't be a property Description. You might just map that property to another table.

首先,EF应该是超过1:1的表映射,所以很可能只是因为你从表A中删除一个列并不意味着对于那个实体,不应该有属性描述。您可能只是将该属性映射到另一个表。

Second, changing a type could just break builds. that's the only rationale there.

其次,改变类型可能会破坏构建。那是唯一的理由。

#2


5  

I've found that, in general, there are still quite a few bugs with the 'Update Model from Database' functionality.

我发现,一般来说,“从数据库更新模型”功能仍然存在很多错误。

Keys are the killer for me - I've yet to have any modification I make to a foreign-key relationship or to add a Primary Key to a table and have the updater work correctly (in that it will give a compile error on the generated code) - but to solve the problem it's a simple matter of deleting the model and re-importing (only takes a minute) - this is less than ideal obviously, but I've never had a failure from a 'fresh' import.

密钥是我的杀手 - 我还没有对外键关系进行任何修改或者将一个主键添加到表中并让更新程序正常工作(因为它会对生成的代码产生编译错误)代码) - 但要解决这个问题,删除模型和重新导入(只需要一分钟)就很简单了 - 这显然不太理想,但我从来没有因为“新鲜”导入而失败。

#3


1  

From the demos of the designer I've seen, it's not a flawless tool. It is a version 1.0 product, so it's bound to have some pain points. The change type is one of them it seems. From watching the designer and the code generation, I figured that one would break either at compile time (not likely) or at run-time (when the model is actually executed).

从我见过的设计师的演示来看,它不是一个完美的工具。它是1.0版本的产品,所以它必然会有一些痛点。改变类型似乎是其中之一。从观察设计器和代码生成开始,我认为在编译时(不太可能)或在运行时(实际执行模型时)会中断。

#4


0  

You need to delete the column by yourself from the designer or the XML file.

您需要自己从设计器或XML文件中删除该列。

#5


0  

As mentioned before, you can just delete the column from the designer. As far as changing the data type of the column: just refresh the model from the database then go to the table mappings and select the column that you changed in the DB. the values on the right represent your model, oddly enough this does not get updated automatically, but just select the column to the right and go to properties and change the data type there. It should become a drop down menu.

如前所述,您只需从设计器中删除该列即可。至于更改列的数据类型:只需从数据库刷新模型,然后转到表映射并选择在DB中更改的列。右边的值代表你的模型,奇怪的是它不会自动更新,只需选择右边的列并转到属性并在那里更改数据类型。它应该成为一个下拉菜单。

Cheers.

Ruddy

#6


0  

I builded similar application like your requested. But my solution was to hard. I will try to tell;

我按照你的要求建立了类似的应用程序。但我的解决方案很难。我会尽力告诉;

  1. You have to create your own database management clases and these objects will be responsible for create, update database schema (I created manually that).

    您必须创建自己的数据库管理clases,这些对象将负责创建,更新数据库模式(我手动创建)。

  2. I saw good article and source code on ADO.NET Team blog then you can also download EDMTools from this blog, it open source. And you can also implement model generation and update routines from that into your project.

    我在ADO.NET Team博客上看到了很好的文章和源代码,然后你也可以从这个博客下载EDMTools,它是开源的。您还可以将模型生成和更新例程实现到项目中。

  3. Finally when the your schema changed you should recreate and bind your model and rebuild your data assembly during runtime. But you have to know most important think, you should tie your data model assembly to your project with loosely coupled (check out this post)

    最后,当您的架构发生更改时,您应该重新创建并绑定模型并在运行时重建数据程序集。但是你必须知道最重要的想法,你应该将你的数据模型程序集与松散耦合的项目联系起来(查看这篇文章)

    Other way, you should wait for EF 4.0 release (it CTP 1 now), they announced that they will provide create,delete,update DatabaseScript functions.

    另外,你应该等待EF 4.0发布(它现在是CTP 1),他们宣布他们将提供创建,删除,更新DatabaseScript功能。

Good lock

#7


0  

The way I'm doing this (and I'm doing all of the things you mention, plus renaming columns) is by making changes to the database and regenerating the EF code using EF Code First.

我这样做的方式(我正在做你提到的所有事情,再加上重命名列)是通过更改数据库并使用EF Code First重新生成EF代码。

I'm not tampering with the EF Code First classes for the good or the bad (including nonsensically named columns for relations) to ease the process.

我没有篡改EF Code First类的好坏(包括关系的无意义命名列)以简化过程。

No designer or ORM schema generator will be able to make changes to your production database if it has constrained data in it. This is why you should always start with checking if your changes to the DB are feasible, try them on a development database and then adapt your code to reflect the changes.

如果生产数据库中的数据受限,则任何设计人员或ORM模式生成器都无法对其进行更改。这就是为什么您应该始终检查您对数据库的更改是否可行,在开发数据库上尝试它们然后调整代码以反映更改。

#1


6  

I would guess that possibly those don't happen because they would break the build for existing code, but that's just a guess on my part.

我猜这可能不会发生,因为它们会打破现有代码的构建,但这只是我的猜测。

Here's my logic:

这是我的逻辑:

First, EF is supposed to be more than 1:1 table mapping, so it's quite possible that just because you are deleting a column from table A doesn't mean that for that entity, there shouldn't be a property Description. You might just map that property to another table.

首先,EF应该是超过1:1的表映射,所以很可能只是因为你从表A中删除一个列并不意味着对于那个实体,不应该有属性描述。您可能只是将该属性映射到另一个表。

Second, changing a type could just break builds. that's the only rationale there.

其次,改变类型可能会破坏构建。那是唯一的理由。

#2


5  

I've found that, in general, there are still quite a few bugs with the 'Update Model from Database' functionality.

我发现,一般来说,“从数据库更新模型”功能仍然存在很多错误。

Keys are the killer for me - I've yet to have any modification I make to a foreign-key relationship or to add a Primary Key to a table and have the updater work correctly (in that it will give a compile error on the generated code) - but to solve the problem it's a simple matter of deleting the model and re-importing (only takes a minute) - this is less than ideal obviously, but I've never had a failure from a 'fresh' import.

密钥是我的杀手 - 我还没有对外键关系进行任何修改或者将一个主键添加到表中并让更新程序正常工作(因为它会对生成的代码产生编译错误)代码) - 但要解决这个问题,删除模型和重新导入(只需要一分钟)就很简单了 - 这显然不太理想,但我从来没有因为“新鲜”导入而失败。

#3


1  

From the demos of the designer I've seen, it's not a flawless tool. It is a version 1.0 product, so it's bound to have some pain points. The change type is one of them it seems. From watching the designer and the code generation, I figured that one would break either at compile time (not likely) or at run-time (when the model is actually executed).

从我见过的设计师的演示来看,它不是一个完美的工具。它是1.0版本的产品,所以它必然会有一些痛点。改变类型似乎是其中之一。从观察设计器和代码生成开始,我认为在编译时(不太可能)或在运行时(实际执行模型时)会中断。

#4


0  

You need to delete the column by yourself from the designer or the XML file.

您需要自己从设计器或XML文件中删除该列。

#5


0  

As mentioned before, you can just delete the column from the designer. As far as changing the data type of the column: just refresh the model from the database then go to the table mappings and select the column that you changed in the DB. the values on the right represent your model, oddly enough this does not get updated automatically, but just select the column to the right and go to properties and change the data type there. It should become a drop down menu.

如前所述,您只需从设计器中删除该列即可。至于更改列的数据类型:只需从数据库刷新模型,然后转到表映射并选择在DB中更改的列。右边的值代表你的模型,奇怪的是它不会自动更新,只需选择右边的列并转到属性并在那里更改数据类型。它应该成为一个下拉菜单。

Cheers.

Ruddy

#6


0  

I builded similar application like your requested. But my solution was to hard. I will try to tell;

我按照你的要求建立了类似的应用程序。但我的解决方案很难。我会尽力告诉;

  1. You have to create your own database management clases and these objects will be responsible for create, update database schema (I created manually that).

    您必须创建自己的数据库管理clases,这些对象将负责创建,更新数据库模式(我手动创建)。

  2. I saw good article and source code on ADO.NET Team blog then you can also download EDMTools from this blog, it open source. And you can also implement model generation and update routines from that into your project.

    我在ADO.NET Team博客上看到了很好的文章和源代码,然后你也可以从这个博客下载EDMTools,它是开源的。您还可以将模型生成和更新例程实现到项目中。

  3. Finally when the your schema changed you should recreate and bind your model and rebuild your data assembly during runtime. But you have to know most important think, you should tie your data model assembly to your project with loosely coupled (check out this post)

    最后,当您的架构发生更改时,您应该重新创建并绑定模型并在运行时重建数据程序集。但是你必须知道最重要的想法,你应该将你的数据模型程序集与松散耦合的项目联系起来(查看这篇文章)

    Other way, you should wait for EF 4.0 release (it CTP 1 now), they announced that they will provide create,delete,update DatabaseScript functions.

    另外,你应该等待EF 4.0发布(它现在是CTP 1),他们宣布他们将提供创建,删除,更新DatabaseScript功能。

Good lock

#7


0  

The way I'm doing this (and I'm doing all of the things you mention, plus renaming columns) is by making changes to the database and regenerating the EF code using EF Code First.

我这样做的方式(我正在做你提到的所有事情,再加上重命名列)是通过更改数据库并使用EF Code First重新生成EF代码。

I'm not tampering with the EF Code First classes for the good or the bad (including nonsensically named columns for relations) to ease the process.

我没有篡改EF Code First类的好坏(包括关系的无意义命名列)以简化过程。

No designer or ORM schema generator will be able to make changes to your production database if it has constrained data in it. This is why you should always start with checking if your changes to the DB are feasible, try them on a development database and then adapt your code to reflect the changes.

如果生产数据库中的数据受限,则任何设计人员或ORM模式生成器都无法对其进行更改。这就是为什么您应该始终检查您对数据库的更改是否可行,在开发数据库上尝试它们然后调整代码以反映更改。