EF中的问题,映射片段,没有默认值,不可空 [英] Issue in EF, mapping fragment,no default value and is not nullable

查看:104
本文介绍了EF中的问题,映射片段,没有默认值,不可空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





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



当我尝试验证模型时,它会产生错误。


错误41错误3023:映射从
开始的片段的问题70:列Companies.Status表中公司必须映射:
没有默认值,不可空。


这是表的映射。 b
$ b



这是HTML视图中的EDMX代码。

 <?xml version =1.0encoding = UTF-8 >?; 
< edmx:Edmx Version =2.0xmlns:edmx =http://schemas.microsoft.com/ado/2008/10/edmx>
<! - EF运行时内容 - >
< edmx:运行时>
<! - SSDL内容 - >
< edmx:StorageModels>
< Schema Namespace =Model1.StoreAlias =SelfProvider =System.Data.SqlClientProviderManifestToken =2008xmlns =http://schemas.microsoft.com/ado/2009/ 02 / edm / ssdlxmlns:store =http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator>
< EntityContainer Name =Model1StoreContainer>

< EntitySet Name =CompaniesEntityType =Model1.Store.Companiesstore:Type =TablesSchema =dbo/>

< / EntityContainer>

< EntityType Name =Companies>
< Key>
< PropertyRef Name =Id/>
< / Key>
< Property Name =IdType =intStoreGeneratedPattern =IdentityNullable =false/>
< Property Name =NameType =nvarchar(max)Nullable =false/>
< Property Name =StatusType =nvarchar(max)Nullable =false/>
< Property Name =__ Disc__Type =nvarcharMaxLength =MaxNullable =false/>
< / EntityType>
< / Schema>< / edmx:StorageModels>
<! - CSDL内容 - >
< edmx:ConceptualModels>
< Schema xmlns =http://schemas.microsoft.com/ado/2008/09/edmxmlns:cg =http://schemas.microsoft.com/ado/2006/04/codegeneration xmlns:store =http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGeneratorNamespace =Model1Alias =Selfxmlns:annotation =http://schemas.microsoft.com / ADO / 2009/02 / EDM /注释>
< EntityContainer Name =Model1Container注释:LazyLoadingEnabled =true>
< EntitySet Name =CompaniesEntityType =Model1.Company/>
< / EntityContainer>
< EntityType Name =CompanyAbstract =true>
< Key>
< PropertyRef Name =Id/>
< / Key>
< Property Type =Int32Name =IdNullable =false注释:StoreGeneratedPattern =Identity/>
< Property Type =StringName =NameNullable =false/>
< / EntityType>
< EntityType Name =LeadBaseType =Model1.Company>
< Property Type =StringName =StatusNullable =false/>
< / EntityType>
< / Schema>
< / edmx:ConceptualModels>
<! - C-S映射内容 - >
< edmx:映射>
< Mapping Space =C-Sxmlns =http://schemas.microsoft.com/ado/2008/09/mapping/cs>
< EntityContainerMapping StorageEntityContainer =Model1StoreContainerCdmEntityContainer =Model1Container>
< EntitySetMapping Name =Companies>
< EntityTypeMapping TypeName =IsTypeOf(Model1.Company)>
< MappingFragment StoreEntitySet =Companies>
< ScalarProperty Name =IdColumnName =Id/>
< ScalarProperty Name =NameColumnName =Name/>
< Condition ColumnName =__ Disc__Value =Company/>
< / MappingFragment>
< / EntityTypeMapping>
< EntityTypeMapping TypeName =Model1.Lead>
< MappingFragment StoreEntitySet =Companies>
< ScalarProperty Name =IdColumnName =Id/>
< ScalarProperty Name =NameColumnName =Name/>
< ScalarProperty Name =StatusColumnName =Status/>
< Condition ColumnName =__ Disc__Value =Lead/>
< / MappingFragment>
< / EntityTypeMapping>
< / EntitySetMapping>
< / EntityContainerMapping>
< / Mapping>< / edmx:Mappings>
< / edmx:Runtime>
<! - EF设计师内容(请勿手动编辑此处) - >
< edmx:Designer xmlns =http://schemas.microsoft.com/ado/2008/10/edmx>
< edmx:Connection>
< DesignerInfoPropertySet>
< DesignerProperty Name =MetadataArtifactProcessingValue =EmbedInOutputAssembly/>
< / DesignerInfoPropertySet>
< / edmx:Connection>
< edmx:选项>
< DesignerInfoPropertySet>
< DesignerProperty Name =ValidateOnBuildValue =true/>
< DesignerProperty Name =EnablePluralizationValue =True/>
< DesignerProperty Name =DatabaseGenerationWorkflowValue =$(VSEFTools)\DBGen\Generate T-SQL Via T4(TPH).xaml/>
< / DesignerInfoPropertySet>
< / edmx:选项>
<! - 图表内容(形状和连接器位置) - >
< edmx:Diagrams>
< Diagram Name =Model1>
< EntityTypeShape EntityType =Model1.CompanyWidth =1.5PointX =2.375PointY =0.875Height =1.2636116536458335/>
< EntityTypeShape EntityType =Model1.LeadWidth =1.5PointX =3.375PointY =2.625Height =1.0992643229166665/>
< InheritanceConnector EntityType =Model1.Lead>
< ConnectorPoint PointX =3.125PointY =2.1386116536458335/>
< ConnectorPoint PointX =3.125PointY =2.325/>
< ConnectorPoint PointX =4.125PointY =2.325/>
< ConnectorPoint PointX =4.125PointY =2.625/>
< / InheritanceConnector>
< / Diagram>
< / edmx:Diagrams>
< / edmx:Designer>
< / edmx:Edmx>

有什么问题?

解决方案

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

 < Property Name =StatusType =nvarchar max)Nullable =falseDefaultValue =/> 

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


I am developing MVC 3 Applicaiton.

I have Model first approch.

I have Company Entity(Abstract). Lead and Customer is inherited from the company entity.

When I tried to validate the model, Its gives an errror.

Error 41 Error 3023: Problem in mapping fragments starting at line 70:Column Companies.Status in table Companies must be mapped: It has no default value and is not nullable.

Here is the mapping of tables.

And Here is the EDMX code in HTML View.

<?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>

Whats is the issue ?

解决方案

Put the DefaultValue attribute on the SSDL Status property. It would look like this (I used the empty string):

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

Since the base entity is abstract you won't be able to create entities of this type so this DefaultValue will not really be used but it should make EF stop complaining.

这篇关于EF中的问题,映射片段,没有默认值,不可空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆