2

我正在開發MVC 3 Applicaiton。EF中的問題,映射片段,沒有默認值,並且不爲空

我有型號第一次接近。

我有公司實體(摘要)。 銷售線索和客戶是從公司實體繼承的。

當我試圖驗證模型時,它給出了一個錯誤。

錯誤41錯誤3023:問題在映射片段起始於線 70:列Companies.Status在表公司必須映射:它有 沒有缺省值和不可爲空。

這裏是表格的映射。

enter image description here

,這裏是在HTML查看EDMX代碼。

<?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="Model1.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"> 
    <EntityContainer Name="Model1StoreContainer"> 

     <EntitySet Name="Companies" EntityType="Model1.Store.Companies" store:Type="Tables" Schema="dbo" /> 

    </EntityContainer> 

    <EntityType Name="Companies"> 
     <Key> 
      <PropertyRef Name="Id" /> 
     </Key> 
     <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" /> 
     <Property Name="Name" Type="nvarchar(max)" Nullable="false" /> 
     <Property Name="Status" Type="nvarchar(max)" Nullable="false" /> 
     <Property Name="__Disc__" Type="nvarchar" MaxLength="Max" Nullable="false" /> 
    </EntityType> 
</Schema></edmx:StorageModels> 
    <!-- CSDL content --> 
    <edmx:ConceptualModels> 
     <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Model1" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"> 
     <EntityContainer Name="Model1Container" annotation:LazyLoadingEnabled="true"> 
      <EntitySet Name="Companies" EntityType="Model1.Company" /> 
      </EntityContainer> 
     <EntityType Name="Company" Abstract="true"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> 
      <Property Type="String" Name="Name" Nullable="false" /> 
     </EntityType> 
     <EntityType Name="Lead" BaseType="Model1.Company" > 
      <Property Type="String" Name="Status" Nullable="false" /> 
     </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="Model1StoreContainer" CdmEntityContainer="Model1Container"> 
     <EntitySetMapping Name="Companies"> 
      <EntityTypeMapping TypeName="IsTypeOf(Model1.Company)"> 
       <MappingFragment StoreEntitySet="Companies"> 
        <ScalarProperty Name="Id" ColumnName="Id" /> 
        <ScalarProperty Name="Name" ColumnName="Name" /> 
        <Condition ColumnName="__Disc__" Value="Company" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      <EntityTypeMapping TypeName="Model1.Lead"> 
       <MappingFragment StoreEntitySet="Companies"> 
        <ScalarProperty Name="Id" ColumnName="Id" /> 
        <ScalarProperty Name="Name" ColumnName="Name" /> 
        <ScalarProperty Name="Status" ColumnName="Status" /> 
        <Condition ColumnName="__Disc__" Value="Lead" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
     </EntitySetMapping> 
    </EntityContainerMapping> 
</Mapping></edmx:Mappings> 
    </edmx:Runtime> 
    <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) --> 
    <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx"> 
    <edmx:Connection> 
     <DesignerInfoPropertySet> 
     <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /> 
     </DesignerInfoPropertySet> 
    </edmx:Connection> 
    <edmx:Options> 
     <DesignerInfoPropertySet> 
     <DesignerProperty Name="ValidateOnBuild" Value="true" /> 
     <DesignerProperty Name="EnablePluralization" Value="True" /> 
     <DesignerProperty Name="DatabaseGenerationWorkflow" Value="$(VSEFTools)\DBGen\Generate T-SQL Via T4 (TPH).xaml" /> 
     </DesignerInfoPropertySet> 
    </edmx:Options> 
    <!-- Diagram content (shape and connector positions) --> 
    <edmx:Diagrams> 
     <Diagram Name="Model1" > 
     <EntityTypeShape EntityType="Model1.Company" Width="1.5" PointX="2.375" PointY="0.875" Height="1.2636116536458335" /> 
     <EntityTypeShape EntityType="Model1.Lead" Width="1.5" PointX="3.375" PointY="2.625" Height="1.0992643229166665" /> 
     <InheritanceConnector EntityType="Model1.Lead" > 
      <ConnectorPoint PointX="3.125" PointY="2.1386116536458335" /> 
      <ConnectorPoint PointX="3.125" PointY="2.325" /> 
      <ConnectorPoint PointX="4.125" PointY="2.325" /> 
      <ConnectorPoint PointX="4.125" PointY="2.625" /> 
     </InheritanceConnector> 
     </Diagram> 
    </edmx:Diagrams> 
    </edmx:Designer> 
</edmx:Edmx> 

這是什麼問題?

回答

7

將DefaultValue屬性放在SSDL狀態屬性上。它看起來像這樣(我用空字符串):

<Property Name="Status" Type="nvarchar(max)" Nullable="false" DefaultValue=""/> 

由於基礎機構是抽象的,你將不能夠創建這種類型的所以這個默認值不會真正被使用的實體,但它應使EF停止抱怨。

0

在您的存儲模型中,Companies.Status列不允許空值。

由於除了Lead實體之外,從公司繼承的所有實體都不會設置Status屬性,因此您需要在Companies.Status列中允許爲null或爲其他實體設置默認值。

相關問題