向实体框架中的现有表添加新列 [英] Adding a New Column to an Existing Table in Entity Framework

查看:161
本文介绍了向实体框架中的现有表添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的数据库中的表中添加了一个新的列。该表已经在现有的实体框架模型中定义。我已经通过了大部分的项目,如何做到这一点,它仍然失败。



有一点背景,这个实体模型在至少3年内还没有更新。所以除了我添加的列之外,我知道当时已经添加了很多其他列,但是从未包含。我在9个月前接手了这个项目,从未能够成功更新该模型。



首次尝试:




  • 在Visual Studio中打开模型

  • 右键点击背景

  • 点击从数据库更新模型...

  • 点击刷新选项卡

  • 所选表格

  • 突出显示具体表格

  • 点击完成



结果:




  • 我模型中数十个表的类已被删除

  • 未更新



第二次尝试




  • 恢复所有来源

  • 与上述相同但

  • 打开更新向导后,单击删除选项卡

  • 选择表

  • 点击完成

  • 所有表已删除

  • 已保存e EF模型/关闭/打开

  • 返回到更新向导添加选项卡

  • 单击表

  • 当我展开一切时,我的表都没有显示

  • 勾选了表级别的复选框



结果




  • 我的表都没有添加回来,但是最初没有
    包括已添加



第三次尝试




  • 还原所有来源

  • 删除两个.tt文件

  • 打开更新向导

  • 点击添加一切



结果






第四次尝试




  • 还原源

  • 从EF模型中删除表

  • 打开的更新向导

  • 单击添加T ables



结果




  • 在我的模型中的几十个表的类被删除

  • 有问题的表未添加回



最终尝试




  • 手动添加实体建模



结果




  • 并且运行,但是从来没有从数据库中检索到值,或者更新了



可以提供的任何帮助或方向将不胜感激,因为我

解决方案

数据库更新模式是硬/慢使用和容易出错。它产生你可能不想/需要的其他东西。所以手动添加所需的列会更好地工作。
我建议你在VS编辑器之外做,因为根据多少个模型/表,它可以非常慢地打开VS中的文件。

  1.所以在Windows Exlorer中,右键单击* .edmx文件,并使用记事本(或记事本++ / Textpad)打开。 

2.搜索文本< EntityType Name =YourTableNameToAddColumn> ;.

3.添加属性< Property Name =YourNewColumnNameType =varcharMaxLength =64/>

4.搜索文本< MappingFragment StoreEntitySet =YourTableNameToAddColumn>

5.将映射添加到新列< ScalarProperty Name =YourNewColumnNameColumnName =YourNewColumnName/>

6.保存* .edmx文件


I have added a new column to a table in my database. The table is already defined in the existing Entity Framework model. I've been through most of the items here on how to do this and it still fails.

A little background, this entity model has not been updated in at least 3 years. So aside from the column I'm adding I know there have been a number of other columns that have been added in that time, but never included. I took over the project about 9 months ago and have never been able to successfully update the model.

First attempt:

  • Opened the Model in Visual Studio
  • Right Clicked on the Background
  • Clicked on "Update Model From Database..."
  • Clicked on the Refresh Tab
  • Selected Tables
  • Highlighted the specific table
  • Clicked Finish

Result:

  • Classes for Dozens of tables that were in my model were deleted
  • The table in question was not updated

Second Attempt

  • Restored all Source
  • Same as above but
  • After opening the Update Wizard, Clicked the Delete Tab
  • Selected Tables
  • Clicked Finish
  • All the Tables were deleted
  • Saved the EF Model/Closed/Opened it
  • Went back to the Update Wizard Add Tab
  • Clicked Tables
  • None of my tables were displayed when I expanded everything
  • Checked the checkbox at the Tables level

Result

  • None of my tables were added back, but anything that was not originally included was added

Third Attempt

  • Restored all source
  • Deleted the two .tt files
  • Opened the Update Wizard
  • Clicked Add for Everything

Result

  • Nothing was recreated, no .tt files or anything else.

Fourth Attempt

  • Restored Source
  • Deleted Table from the EF Model
  • Opened Update Wizard
  • Clicked Add Tables

Results

  • Classes for Dozens of tables that were in my model were deleted
  • The table in question was not added back

Final Attempt

  • Added entity manually to model

Result

  • Code all compiled and ran, but values were never retrieved from the DB or updated

Any help or direction that could be provided would be greatly appreciated as I'm at a critical point and have to get the model updated.

解决方案

The "Update Model from Database" is hard/slow to use and is prone to errors. It generates other stuff that you probably don't want/need. So manually adding the column that you need will work better. I suggest you do it outside the VS editor since depending on how many models/tables, it can be very slow opening the file in VS.

 1. So in Windows Exlorer,right click on the *.edmx file and open with Notepad (or Notepad++/Textpad).

 2. Search for the text <EntityType Name="YourTableNameToAddColumn">.

 3. Add the property <Property Name="YourNewColumnName" Type="varchar" MaxLength="64" />

 4. Search for the text <MappingFragment StoreEntitySet="YourTableNameToAddColumn">

 5. Add mapping to the new column <ScalarProperty Name="YourNewColumnName" ColumnName="YourNewColumnName"/>

 6. Save the *.edmx file

这篇关于向实体框架中的现有表添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆