更新:重命名列
我看到您的问题已经改变,因为您在询问重命名列,所以我正在更新我的答案。
让我们按照您的新示例,通过 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文件,去掉<!-- REMOVE --->后面的三个元素,如下图:
<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
- 在 VisualStudio 之外,例如PsPad、NotePad++ 等。
- 或者在 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,双击它,然后像以前一样“从数据库更新模型”。