1

使用实体框架,我有一个数据库,其概念模型不同于物理存储模型。

当我要求模型从模型生成数据库时,会根据概念模型创建一个 SQL 脚本。当我要求从数据库更新模型时,它将附加所有数据库列(并且不查看 MSL 映射)。

该表具有以下列:

[adr_Id] [int] IDENTITY(1,1) NOT NULL, 
[adr_Name] [nvarchar](max) NULL, 
[adr_Street] [nvarchar](max) NULL, 
[adr_City] [nvarchar](max) NULL,

这是.msdx文件:`

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
    <Schema Namespace="AdressenModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
  <EntityContainer Name="AdressenModelStoreContainer">
    <EntitySet Name="Adres" EntityType="AdressenModel.Store.Adres" store:Type="Tables" Schema="dbo" />
  </EntityContainer>
  <EntityType Name="Adres">
    <Key>
      <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
    <Property Name="Straat" Type="nvarchar(max)" Nullable="true" />
    <Property Name="Naam" Type="nvarchar(max)" Nullable="true" />
    <Property Name="Woonplaats" Type="nvarchar(max)" Nullable="true" />
  </EntityType>
</Schema></edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="AdressenModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
        <EntityContainer Name="NAWContext" annotation:LazyLoadingEnabled="true">
          <EntitySet Name="Adres" EntityType="AdressenModel.Adres" />
        </EntityContainer>
        <EntityType Name="Adres">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Type="String" Name="Straat" MaxLength="Max" FixedLength="false" Unicode="true" />
          <Property Type="String" Name="Naam" MaxLength="Max" FixedLength="false" Unicode="true" />
          <Property Type="String" Name="Woonplaats" MaxLength="Max" FixedLength="false" Unicode="true" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
    <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
  <EntityContainerMapping StorageEntityContainer="AdressenModelStoreContainer" CdmEntityContainer="NAWContext">
    <EntitySetMapping Name="Adres">
      <EntityTypeMapping TypeName="IsTypeOf(AdressenModel.Adres)">
        <MappingFragment StoreEntitySet="Adres">
          <ScalarProperty Name="Id" ColumnName="Id" />
          <ScalarProperty Name="Straat" ColumnName="Straat" />
          <ScalarProperty Name="Naam" ColumnName="Naam" />
          <ScalarProperty Name="Woonplaats" ColumnName="Woonplaats" />
        </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/2008/10/edmx">
    <Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </Connection>
    <Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
        <DesignerProperty Name="EnablePluralization" Value="True" />
        <DesignerProperty Name="IncludeForeignKeysInModel" Value="True" />
      </DesignerInfoPropertySet>
    </Options>
    <!-- Diagram content (shape and connector positions) -->
    <Diagrams>
      <Diagram Name="NAWContext">
        <EntityTypeShape EntityType="AdressenModel.Adres" Width="1.5" PointX="0.75" PointY="0.75" Height="1.787985026041667" IsExpanded="true" />
      </Diagram>
    </Diagrams>
  </Designer>
</edmx:Edmx>`

最后是当我执行“从模型生成数据库”时生成的 SQL 脚本:

-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 12/07/2012 14:33:20
-- Generated from EDMX file: C:\Users\w.wilschut\Downloads\502259 Code Ch31\EFSamples\Patient4\NAWContext.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Adressen];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Adres]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Adres];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Adres'
CREATE TABLE [dbo].[Adres] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Straat] nvarchar(max)  NULL,
    [Naam] nvarchar(max)  NULL,
    [Woonplaats] nvarchar(max)  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Adres'
ALTER TABLE [dbo].[Adres]
ADD CONSTRAINT [PK_Adres]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------
4

0 回答 0