Must Be Mapped. It has no default value and is not nullable

Haytham Bustami

I'm just starting to learn EF. The problem I'm facing is with TPH. The example below is from the EF recipes by apress. The table is basically this:

CREATE TABLE [Chapter2].[Employee](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[EmployeeType] [int] NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[Salary] [decimal](18, 0) NOT NULL,
[Wage] [decimal](18, 0) NOT NULL,
 CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED 
(
    [EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

After creating table and importing it into EF. I made two entities, one FullTimeEmployee and HourlyEmployee. I'm setting the condition under employee type to 1 for the full time one and 2 for the hourly one. I'm, of course deleting the property from the main Employee entity.

<edmx:Mappings>
  <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
    <EntityContainerMapping StorageEntityContainer="EFRecipesModel1StoreContainer" CdmEntityContainer="EFRecipesEntities1">
      <EntitySetMapping Name="Employees">
        <EntityTypeMapping TypeName="IsTypeOf(EFRecipesModel1.Employee)">
          <MappingFragment StoreEntitySet="Employee">
            <ScalarProperty Name="EmployeeId" ColumnName="EmployeeId" />
            <ScalarProperty Name="LastName" ColumnName="LastName" />
            <ScalarProperty Name="FirstName" ColumnName="FirstName" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="IsTypeOf(EFRecipesModel1.FullTimeEmployee)">
          <MappingFragment StoreEntitySet="Employee">
            <ScalarProperty Name="EmployeeId" ColumnName="EmployeeId" />
            <ScalarProperty Name="Salary" ColumnName="Salary" />
            <Condition ColumnName="EmployeeType" Value="1" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="IsTypeOf(EFRecipesModel1.HourlyEmployee)">
          <MappingFragment StoreEntitySet="Employee">
            <ScalarProperty Name="EmployeeId" ColumnName="EmployeeId" />
            <ScalarProperty Name="Wage" ColumnName="Wage" />
            <Condition ColumnName="EmployeeType" Value="2" />
          </MappingFragment>
        </EntityTypeMapping>
      </EntitySetMapping>
    </EntityContainerMapping>
  </Mapping>
</edmx:Mappings>

THe error I'm getting is:

Error3023: Problem in maaping fragments starting at line 51 column: Employee.Salary in table Employee must be mapped. It has no default value and is not nullable.

I read around and saw a suggestino up update the SSDL which in a way didn't make sense to me:

    <edmx:StorageModels>
  <Schema Namespace="EFRecipesModel1.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2012" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
    <EntityType Name="Employee">
      <Key>
        <PropertyRef Name="EmployeeId" />
      </Key>
      <Property Name="EmployeeId" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
      <Property Name="EmployeeType" Type="int" Nullable ="false" DefaultValue="1"/>
      <Property Name="FirstName" Type="nvarchar" MaxLength="50" Nullable="false" />
      <Property Name="LastName" Type="nvarchar" MaxLength="50" Nullable="false" />
      <Property Name="Salary" Type="decimal" Precision="18" Scale="0" Nullable="false" />
      <Property Name="Wage" Type="decimal" Precision="18" Scale="0" Nullable="false" />
    </EntityType>
    <EntityContainer Name="EFRecipesModel1StoreContainer">
      <EntitySet Name="Employee" EntityType="Self.Employee" Schema="Chapter2" store:Type="Tables" />
    </EntityContainer>
  </Schema>
</edmx:StorageModels>

note that I added the Nullable and Default value for EmployeeType. This still doesn't solve the problem.

Could I get some help as to why I'm having such a problem mapping properly?

Gert Arnold

To me it makes more sense to add DefaultValue to Salary and Wage in the store model:

<Property Name="Salary" Type="decimal" Nullable="false" DefaultValue="0"/>
<Property Name="Wage" Type="decimal" Nullable="false" DefaultValue="0"/>

And also in the conceptual model (in the edmx designer).

It would also make sense to add them as default constraints to the fields in the database (although EF does not copy these constraints to the edmx when the model is generated from the database).

Anyhow, the fields must have a default value since they are not nullable in the database, and none of the concrete entities supply values for both fields.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the default value of the nullable type "int?" (including question mark)?

From Dev

Default parameter for value must be a compile time constant?

From Dev

Nullable objects must have value error

From Dev

Linq: Nullable Object must have Value

From Dev

Nullable object must have a value. VB.NET

From Dev

How to tell if a Map has a default value?

From Dev

ASP.NET MVC 5 Razor throws 'Nullable object must have a value' even when the object has value

From Dev

Nullable object must have a value datetime

From Dev

Should I give it a Default Value or make it Nullable ?

From Dev

Anonymous Type "Nullable object must have a value" Error

From Dev

Python Runtime error (SyntaxErrorException): default value must be specified here

From Dev

Parameter @code1 has no default value

From Dev

best method to solve Nullable object must have a value in datetime

From Dev

Add nullable column to SQL Server with default value of null

From Dev

The type 'MyObject' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'

From Dev

"Nullable object must have a value" when Where uses a method?

From Dev

Get default value if key has falsy value in dict

From Dev

Nullable object must have a value while using datepicker control with dropdown control

From Dev

Parameter has no default value , database c#

From Dev

Why must a function with a single parameter value must have default values specified for each parameter afterwards?

From Dev

Decimal? - Nullable object must have a value

From Dev

Makefile: Default Value of Variable that is set but has null value

From Dev

Cast to value type 'Enum' failed as the materialized value is null. Either the result type's param or query must use nullable type

From Dev

How to assign default value when array has no value to compare in angularjs

From Dev

Does CAML Query has a default RowLimit value?

From Dev

A given value must be the value that appears by default in a dynamic dropdown - JAVA

From Dev

"Nullable object must have a value" exception after checking for null on a non-primitive/non-struct object

From Dev

Key properties of all entity types must be mapped to the same non-nullable columns returned by the storage function

From Dev

get value of the mapped value within a mapped value

Related Related

  1. 1

    What is the default value of the nullable type "int?" (including question mark)?

  2. 2

    Default parameter for value must be a compile time constant?

  3. 3

    Nullable objects must have value error

  4. 4

    Linq: Nullable Object must have Value

  5. 5

    Nullable object must have a value. VB.NET

  6. 6

    How to tell if a Map has a default value?

  7. 7

    ASP.NET MVC 5 Razor throws 'Nullable object must have a value' even when the object has value

  8. 8

    Nullable object must have a value datetime

  9. 9

    Should I give it a Default Value or make it Nullable ?

  10. 10

    Anonymous Type "Nullable object must have a value" Error

  11. 11

    Python Runtime error (SyntaxErrorException): default value must be specified here

  12. 12

    Parameter @code1 has no default value

  13. 13

    best method to solve Nullable object must have a value in datetime

  14. 14

    Add nullable column to SQL Server with default value of null

  15. 15

    The type 'MyObject' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'

  16. 16

    "Nullable object must have a value" when Where uses a method?

  17. 17

    Get default value if key has falsy value in dict

  18. 18

    Nullable object must have a value while using datepicker control with dropdown control

  19. 19

    Parameter has no default value , database c#

  20. 20

    Why must a function with a single parameter value must have default values specified for each parameter afterwards?

  21. 21

    Decimal? - Nullable object must have a value

  22. 22

    Makefile: Default Value of Variable that is set but has null value

  23. 23

    Cast to value type 'Enum' failed as the materialized value is null. Either the result type's param or query must use nullable type

  24. 24

    How to assign default value when array has no value to compare in angularjs

  25. 25

    Does CAML Query has a default RowLimit value?

  26. 26

    A given value must be the value that appears by default in a dynamic dropdown - JAVA

  27. 27

    "Nullable object must have a value" exception after checking for null on a non-primitive/non-struct object

  28. 28

    Key properties of all entity types must be mapped to the same non-nullable columns returned by the storage function

  29. 29

    get value of the mapped value within a mapped value

HotTag

Archive