【问题标题】:Making Entity Framework Aware of Tables/Column Renames使实体框架知道表/列重命名
【发布时间】:2018-10-05 15:12:37
【问题描述】:

重命名表或列时,如何确保 edmx 的“从数据库更新模型”识别新的表结构?

MyTable 只有 2 列。

ID
name

重构 --> 重命名 (Ctrl R + R)

myTableID
fullName

以下哪一项是“正确”的对应 edmx 更改,因此应用程序可以正常工作而不会出现运行时/编译错误

  1. 重构 --> 重命名 Edmx 文件或
  2. 右键单击 --> 从数据库更新模型,删除旧项目或
  3. 手动创建、删除和验证 edmx

如您所见,这是一个非常简单的重命名。

编译错误

运行时错误: EFControl _current 错误 CS0103:当前上下文中不存在名称“_current”

【问题讨论】:

  • 你解决了吗?
  • 不。我至少失败了7次。只是还不知道有什么收获。

标签: c# sql-server winforms entity-framework edmx


【解决方案1】:

更新:重命名列 我看到您的问题已经改变,因为您在询问重命名列,所以我正在更新我的答案。 让我们按照您的新示例,通过 T-SQL 创建您的表:

CREATE TABLE [dbo].MyTable
(
    [Id] INT NOT NULL PRIMARY KEY, 
    [name] NCHAR(10) NULL
)

实体框架 (EF) 将为您创建 MyTable.cs 文件:

public partial class MyTable
{
    public int Id { get; set; }
    public string name { get; set; }
}

这是 EDMX 的主要部分:

<edmx:ConceptualModels>
  <Schema Namespace="database1Model" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
    <EntityContainer Name="database1Entities" annotation:LazyLoadingEnabled="true" >
      <EntitySet Name="MyTables" EntityType="database1Model.MyTable" />
    </EntityContainer>
    <EntityType Name="MyTable">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Name="Id" Type="Int32" Nullable="false" />
      <Property Name="name" Type="String" MaxLength="10" FixedLength="true" Unicode="true" />
    </EntityType>
  </Schema>
</edmx:ConceptualModels>

然后你重命名你的列,使用例如Visual Studio Server Explorer,它将生成这个 T-SQL(我没有在 MyTable 中插入任何数据):

CREATE TABLE [dbo].[MyTable] (
    [myTableID]   INT        NOT NULL,
    [fullName] NCHAR (10) NULL,
    PRIMARY KEY CLUSTERED ([myTableID] ASC)
);

此时,如果您从数据库中更新您的 EDMX,您将得到:

所以,如果你打开你的EDMX文件,去掉&lt;!-- REMOVE ---&gt;后面的三个元素,如下图:

<edmx:ConceptualModels>
  <Schema Namespace="database1Model" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
    <EntityContainer Name="database1Entities" annotation:LazyLoadingEnabled="true" >
      <EntitySet Name="MyTables" EntityType="database1Model.MyTable" />
    </EntityContainer>
    <EntityType Name="MyTable">
      <Key>
        <PropertyRef Name="Id" /><!-- REMOVE -->
        <PropertyRef Name="myTableID" />
      </Key>
      <Property Name="Id" Type="Int32" Nullable="false" /><!-- REMOVE -->
      <Property Name="name" Type="String" MaxLength="10" FixedLength="true" Unicode="true" /><!-- REMOVE -->
      <Property Name="myTableID" Type="Int32" Nullable="false" />
      <Property Name="fullName" Type="String" MaxLength="10" FixedLength="true" Unicode="true" />
    </EntityType>
  </Schema>
</edmx:ConceptualModels>

并从您的模型进行更新,一切顺利。甚至您自动生成的文件 MyTable.cs 也将更新为新名称:

public partial class MyTable
{
    public int myTableID { get; set; }
    public string fullName { get; set; }
}

回答原始问题:重命名表格 一种方法是编辑您的 .EDMX 文件,该文件是 Entity Framework 为您创建的,它存储有关将数据库映射到您的对象的所有信息。 该文件可以在项目的根文件夹中找到,即 .csproj 文件所在的位置,它只是一个可以使用文本编辑器打开的 XML

  1. 在 VisualStudio 之外,例如PsPad、NotePad++ 等。
  2. 或者在 Visual Studio 中右键单击它,选择“打开方式...”并选择“XML(文本)编辑器”。

我猜你的Foo 类对应于Foo 表;后者现在可能是FooNew 或类似的东西。

要重命名表但保留 Foo 类,只需查找 EDMX 文件的这一部分:

<!-- SSDL content -->
<edmx:StorageModels>
...
    <EntitySet Name="Foo" ... Schema="dbo" ... />
...
</edmx:StorageModels>

EntitySet 内添加Table="FooNew" 作为属性,正如question 的答案所建议的那样:

<EntitySet Name="Foo" ... Schema="dbo" ... Table="FooNew" />

此时,关闭并保存;重新打开 edmx,双击它,然后像以前一样“从数据库更新模型”。

【讨论】:

  • 嘿 Francesco,这仍然给我一个实体框架映射错误。
  • 会发生什么?你能提供更多细节吗?
  • 根实体框架映射的运行时错误:EFControl _current 错误 CS0103:当前上下文中不存在名称“_current”
  • @DLNarasimhan 我更新了我的答案(并对其进行了测试,如您所见)。
猜你喜欢
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 2022-08-21
相关资源
最近更新 更多