.net实体类更新的方法(没有办法的办法)
采用此方法时,强烈建议,更新前,先将model层的代码拷贝出来,或是上传到版本控制工具上。
整体思路:
1.model层的代码备份
2.删除以前的edmx文件,新增新的edmx(只含有想添加的表)
3.拷贝出生成的文件(edmx对应的xml和实体类.cs)
4.删除刚生成的edmx,恢复成原始状态
5.以xml打开原始状态的edmx文件,将新生成的edmx相应节点放进原始edmx文件中
6.将拷贝出的实体类.cs放到model层中
7.找到继承DbContext的类,声明生成的对应的实体类
1.自行选择model层的备份方式
2.删除.edmx文件-->右击model文件夹-->添加-->新建项-->搜索ado-->输入名称-->添加-->来自数据库EF设计器-->下一步-->是
选中想更新的表或视图,单击完成
3.拷贝出生成的文件
右击.edmx文件--->选择打开方式--->选择xml文本编辑器--->确定
将生成的edmx文件以xml形式读取,全都拷贝到记事本中
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="OnlineEDUModel.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2008" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<!--生成过程中发现错误:
警告 6002: 表/视图“OnlineEDU.dbo.view_area”未定义主键。已推断出该键,并将定义创建为只读的表/视图。-->
<EntityType Name="view_area">
<Key>
<PropertyRef Name="UserId" />
<PropertyRef Name="RoleId" />
<PropertyRef Name="username" />
<PropertyRef Name="Credit" />
</Key>
<Property Name="col" Type="bigint" />
<Property Name="UserId" Type="nvarchar" MaxLength="128" Nullable="false" />
<Property Name="RoleId" Type="nvarchar" MaxLength="128" Nullable="false" />
<Property Name="username" Type="nvarchar" MaxLength="32" Nullable="false" />
<Property Name="school" Type="nvarchar" MaxLength="32" />
<Property Name="firsttime" Type="datetime" />
<Property Name="Credit" Type="bigint" Nullable="false" />
<Property Name="Province" Type="nvarchar" MaxLength="50" />
<Property Name="City" Type="nvarchar" MaxLength="50" />
<Property Name="Area" Type="nvarchar" MaxLength="50" />
</EntityType>
<!--生成过程中发现错误:
警告 6002: 表/视图“OnlineEDU.dbo.view_teacher”未定义主键。已推断出该键,并将定义创建为只读的表/视图。-->
<EntityType Name="view_teacher">
<Key>
<PropertyRef Name="UserId" />
<PropertyRef Name="RoleId" />
<PropertyRef Name="username" />
<PropertyRef Name="Credit" />
</Key>
<Property Name="col" Type="bigint" />
<Property Name="UserId" Type="nvarchar" MaxLength="128" Nullable="false" />
<Property Name="RoleId" Type="nvarchar" MaxLength="128" Nullable="false" />
<Property Name="username" Type="nvarchar" MaxLength="32" Nullable="false" />
<Property Name="school" Type="nvarchar" MaxLength="32" />
<Property Name="firsttime" Type="datetime" />
<Property Name="Province" Type="nvarchar" MaxLength="50" />
<Property Name="City" Type="nvarchar" MaxLength="50" />
<Property Name="Area" Type="nvarchar" MaxLength="50" />
<Property Name="Credit" Type="bigint" Nullable="false" />
</EntityType>
<EntityContainer Name="OnlineEDUModelStoreContainer">
<EntitySet Name="view_area" EntityType="Self.view_area" store:Type="Views" store:Schema="dbo">
<DefiningQuery>SELECT
[view_area].[col] AS [col],
[view_area].[UserId] AS [UserId],
[view_area].[RoleId] AS [RoleId],
[view_area].[username] AS [username],
[view_area].[school] AS [school],
[view_area].[firsttime] AS [firsttime],
[view_area].[Credit] AS [Credit],
[view_area].[Province] AS [Province],
[view_area].[City] AS [City],
[view_area].[Area] AS [Area]
FROM [dbo].[view_area] AS [view_area]</DefiningQuery>
</EntitySet>
<EntitySet Name="view_teacher" EntityType="Self.view_teacher" store:Type="Views" store:Schema="dbo">
<DefiningQuery>SELECT
[view_teacher].[col] AS [col],
[view_teacher].[UserId] AS [UserId],
[view_teacher].[RoleId] AS [RoleId],
[view_teacher].[username] AS [username],
[view_teacher].[school] AS [school],
[view_teacher].[firsttime] AS [firsttime],
[view_teacher].[Province] AS [Province],
[view_teacher].[City] AS [City],
[view_teacher].[Area] AS [Area],
[view_teacher].[Credit] AS [Credit]
FROM [dbo].[view_teacher] AS [view_teacher]</DefiningQuery>
</EntitySet>
</EntityContainer>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="OnlineEDUModel" 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">
<EntityType Name="view_area">
<Key>
<PropertyRef Name="UserId" />
<PropertyRef Name="RoleId" />
<PropertyRef Name="username" />
<PropertyRef Name="Credit" />
</Key>
<Property Name="col" Type="Int64" />
<Property Name="UserId" Type="String" MaxLength="128" FixedLength="false" Unicode="true" Nullable="false" />
<Property Name="RoleId" Type="String" MaxLength="128" FixedLength="false" Unicode="true" Nullable="false" />
<Property Name="username" Type="String" MaxLength="32" FixedLength="false" Unicode="true" Nullable="false" />
<Property Name="school" Type="String" MaxLength="32" FixedLength="false" Unicode="true" />
<Property Name="firsttime" Type="DateTime" Precision="3" />
<Property Name="Credit" Type="Int64" Nullable="false" />
<Property Name="Province" Type="String" MaxLength="50" FixedLength="false" Unicode="true" />
<Property Name="City" Type="String" MaxLength="50" FixedLength="false" Unicode="true" />
<Property Name="Area" Type="String" MaxLength="50" FixedLength="false" Unicode="true" />
</EntityType>
<EntityType Name="view_teacher">
<Key>
<PropertyRef Name="UserId" />
<PropertyRef Name="RoleId" />
<PropertyRef Name="username" />
<PropertyRef Name="Credit" />
</Key>
<Property Name="col" Type="Int64" />
<Property Name="UserId" Type="String" MaxLength="128" FixedLength="false" Unicode="true" Nullable="false" />
<Property Name="RoleId" Type="String" MaxLength="128" FixedLength="false" Unicode="true" Nullable="false" />
<Property Name="username" Type="String" MaxLength="32" FixedLength="false" Unicode="true" Nullable="false" />
<Property Name="school" Type="String" MaxLength="32" FixedLength="false" Unicode="true" />
<Property Name="firsttime" Type="DateTime" Precision="3" />
<Property Name="Province" Type="String" MaxLength="50" FixedLength="false" Unicode="true" />
<Property Name="City" Type="String" MaxLength="50" FixedLength="false" Unicode="true" />
<Property Name="Area" Type="String" MaxLength="50" FixedLength="false" Unicode="true" />
<Property Name="Credit" Type="Int64" Nullable="false" />
</EntityType>
<EntityContainer Name="OnlineEDUEntities1" annotation:LazyLoadingEnabled="true">
<EntitySet Name="view_area" EntityType="Self.view_area" />
<EntitySet Name="view_teacher" EntityType="Self.view_teacher" />
</EntityContainer>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
<EntityContainerMapping StorageEntityContainer="OnlineEDUModelStoreContainer" CdmEntityContainer="OnlineEDUEntities1">
<EntitySetMapping Name="view_area">
<EntityTypeMapping TypeName="OnlineEDUModel.view_area">
<MappingFragment StoreEntitySet="view_area">
<ScalarProperty Name="col" ColumnName="col" />
<ScalarProperty Name="UserId" ColumnName="UserId" />
<ScalarProperty Name="RoleId" ColumnName="RoleId" />
<ScalarProperty Name="username" ColumnName="username" />
<ScalarProperty Name="school" ColumnName="school" />
<ScalarProperty Name="firsttime" ColumnName="firsttime" />
<ScalarProperty Name="Credit" ColumnName="Credit" />
<ScalarProperty Name="Province" ColumnName="Province" />
<ScalarProperty Name="City" ColumnName="City" />
<ScalarProperty Name="Area" ColumnName="Area" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<EntitySetMapping Name="view_teacher">
<EntityTypeMapping TypeName="OnlineEDUModel.view_teacher">
<MappingFragment StoreEntitySet="view_teacher">
<ScalarProperty Name="col" ColumnName="col" />
<ScalarProperty Name="UserId" ColumnName="UserId" />
<ScalarProperty Name="RoleId" ColumnName="RoleId" />
<ScalarProperty Name="username" ColumnName="username" />
<ScalarProperty Name="school" ColumnName="school" />
<ScalarProperty Name="firsttime" ColumnName="firsttime" />
<ScalarProperty Name="Province" ColumnName="Province" />
<ScalarProperty Name="City" ColumnName="City" />
<ScalarProperty Name="Area" ColumnName="Area" />
<ScalarProperty Name="Credit" ColumnName="Credit" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
<Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</Connection>
<Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
<DesignerProperty Name="EnablePluralization" Value="false" />
<DesignerProperty Name="IncludeForeignKeysInModel" Value="true" />
<DesignerProperty Name="UseLegacyProvider" Value="false" />
<DesignerProperty Name="CodeGenerationStrategy" Value="无" />
</DesignerInfoPropertySet>
</Options>
<!-- Diagram content (shape and connector positions) -->
<Diagrams></Diagrams>
</Designer>
</edmx:Edmx>
将生成的这两个实体类拷贝出来
4.删除生成的edmx文件,将之前备份的代码更新下来
5.以xml的形式编辑以前的edmx文件,把刚才生成的xml节点放到相应的位置上。
6.将生成的实体类.cs(view_area.cs,view_teacher.cs)拷贝到model中
7.找到继承DbContext的类,添加下述代码