【问题标题】:Issue in EF, mapping fragment,no default value and is not nullableEF 中的问题,映射片段,无默认值且不可为空
【发布时间】:2012-10-02 05:25:37
【问题描述】:

我正在开发 MVC 3 应用程序。

我有模型优先的方法。

我有公司实体(摘要)。 Lead 和 Customer 继承自公司实体。

当我尝试验证模型时,它给出了一个错误。

错误 41 错误 3023:从行开始映射片段时出现问题 70:Column Companies.Status in table Companies must be mapped: 它有 没有默认值,不能为空。

这是表的映射。

这是 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>

有什么问题吗?

【问题讨论】:

    标签: entity-framework entity-framework-4 entity-framework-4.1 entity-relationship


    【解决方案1】:

    将 DefaultValue 属性放在 SSDL 状态属性上。它看起来像这样(我使用了空字符串):

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

    由于基本实体是抽象的,您将无法创建这种类型的实体,因此不会真正使用此 DefaultValue,但它应该让 EF 停止抱怨。

    【讨论】:

    • 这也可以在 UI/Designer 的某个地方完成吗?
    • 虽然这为我解决了 OP 中的问题,但在运行代码时,我发现我无法将记录插入表中,因为 EF 尝试将 null 放入列中,尽管默认值。我还尝试在设计器中设置默认值,但问题仍然存在。有谁知道解决方案,还是强制所有列都标记为可为空?
    【解决方案2】:

    在您的存储模型中,Company.Status 列不允许空值。

    由于从 Company 继承的所有实体(Lead 实体除外)都不会设置 Status 属性,因此您需要在 Companies.Status 列中允许 null 或设置用于其他实体的默认值。

    【讨论】:

      猜你喜欢
      • 2011-05-26
      • 2013-09-19
      • 2020-02-08
      • 2011-08-16
      • 1970-01-01
      • 2011-08-07
      • 2018-12-21
      • 1970-01-01
      相关资源
      最近更新 更多