C# 使实体框架了解表/列重命名
重命名表或列时,如何确保edmx的“从数据库更新模型”识别新的表结构 MyTable只有两列C# 使实体框架了解表/列重命名,c#,sql-server,winforms,entity-framework,edmx,C#,Sql Server,Winforms,Entity Framework,Edmx,重命名表或列时,如何确保edmx的“从数据库更新模型”识别新的表结构 MyTable只有两列 ID name 重构-->重命名(Ctrl-R+R) 以下哪项是对应于edmx的“正确”更改,因此应用程序工作时不会出现运行时/编译错误 在Edmx文件上重构-->重命名,或 右键单击-->从数据库更新模型,删除旧项或 手动创建、删除和验证edmx 如您所见,这是一个非常简单的重命名 编译错误 运行时错误: EFControl\u current error CS0103:名称“\u current”
ID
name
重构-->重命名(Ctrl-R+R)
以下哪项是对应于edmx的“正确”更改,因此应用程序工作时不会出现运行时/编译错误
EFControl\u current error CS0103:名称“\u current”在当前上下文中不存在更新:重命名列 我看到你的问题已经改变了,因为你正在询问关于重命名列的问题,所以我正在更新我的答案。 让我们按照您的新示例,通过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>
此时,如果从数据库更新EDMX,您将得到以下结果:
因此,如果您打开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" /><!-- 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>
对原始问题的回答:重命名表格 一种方法是编辑.EDMX文件,该文件是为您创建的实体框架,它存储了有关将数据库映射到对象的所有信息。 该文件可以在项目的根文件夹中找到,即.csproj文件所在的位置,它只是一个可以用文本编辑器打开的XML
Foo
类对应于Foo
表;后者现在可能是新的或类似的东西
要重命名表但保留Foo
类,只需查找EDMX文件的这一部分:
<!-- SSDL content -->
<edmx:StorageModels>
...
<EntitySet Name="Foo" ... Schema="dbo" ... />
...
</edmx:StorageModels>
此时,关闭并保存;重新打开edmx双击它,然后像以前一样“从数据库更新模型”。您解决了这个问题吗?没有。我至少失败了7次。只是还不知道问题是什么。嘿,Francesco,这仍然给了我一个关于实体框架映射的错误。发生了什么?您可以提供更多详细信息吗?根实体框架映射的运行时错误:EFControl\u current Error CS0103:当前映射中不存在名称“\u current”context@DLNarasimhan我更新了我的答案(如您所见,并对其进行了测试)。
<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>
public partial class MyTable
{
public int myTableID { get; set; }
public string fullName { get; set; }
}
<!-- SSDL content -->
<edmx:StorageModels>
...
<EntitySet Name="Foo" ... Schema="dbo" ... />
...
</edmx:StorageModels>
<EntitySet Name="Foo" ... Schema="dbo" ... Table="FooNew" />