在重命名表或列时,如何确保edmx的“数据库更新模型”识别新的表结构?
MyTable只有2列。
ID
name
重构->重命名(Ctrl R+ R)
myTableID
fullName
下面哪个是相应的edmx更改的“右”,因此应用程序在没有运行时/编译错误的情况下工作。
正如您所看到的,这是一个非常简单的重命名。
编译错误
运行时错误: EFControl _current Error CS0103:名称'_current‘在当前上下文中不存在
发布于 2018-04-25 01:56:19
更新:重命名列--我看到您的问题已经改变了,因为您问的是重命名列,所以我正在更新我的答案。让我们按照您的新示例,通过T创建您的表:
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 (我没有在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文件、为您创建的实体框架以及它存储有关将数据库映射到对象的所有信息。这个文件可以在项目的根文件夹(即.csproj文件所在的位置)中找到,它只是一个可以用文本编辑器打开的XML。
我猜您的Foo
类对应于Foo
表;后者现在可能是FooNew
或类似的东西。
要重命名表,但保留Foo
类,只需查找EDMX文件的这一部分:
<!-- SSDL content -->
<edmx:StorageModels>
...
<EntitySet Name="Foo" ... Schema="dbo" ... />
...
</edmx:StorageModels>
在EntitySet
中添加Table="FooNew"
as属性,如对question的答复所建议的那样
<EntitySet Name="Foo" ... Schema="dbo" ... Table="FooNew" />
此时,关闭并保存;重新打开edmx,双击它,然后像前面那样“从数据库更新模型”。
https://stackoverflow.com/questions/50017245
复制