1

I have a simple C# data model that I am using with Entity Framework Core and OData that is being built with Nullable Reference Types "enable".

 public record Country
{
    [Key]
    [DisplayName("ISO Code")]
    [MaxLength(2)]
    [MinLength(2)]
    public string ISOCode { get; init; } = string.Empty;

    public string Name { get; init; } = string.Empty;
}

When building the EF Core and SQL database the property Name is created NOT NULL:

CREATE TABLE [dbo].[Country] (
[ISOCode] NVARCHAR (2)   NOT NULL,
[Name]    NVARCHAR (MAX) NOT NULL,
CONSTRAINT [PK_Country] PRIMARY KEY CLUSTERED ([ISOCode] ASC));

However, when I build the OData endpoint there is no NULLABLE facet:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
    <edmx:DataServices>
        <Schema Namespace="DiveShopService.Models" xmlns="http://docs.oasis-open.org/odata/ns/edm">
            <EntityType Name="Country">
                <Key>
                    <PropertyRef Name="ISOCode" />
                </Key>
                <Property Name="ISOCode" Type="Edm.String" Nullable="false" MaxLength="2" />
                <Property Name="Name" Type="Edm.String" />
            </EntityType>
        </Schema>
        <Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
            <EntityContainer Name="Container">
                <EntitySet Name="Countries" EntityType="DiveShopService.Models.Country" />
            </EntityContainer>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

It should be <Property Name="Name" Type="Edm.String" Nullable="false" />

Is there some configuration option to cause OData to understand Nullable Reference Types?

Visual Studio 16.8.4 Microsoft.EntityFrameworkCore v5.0.2 Microsoft.AspNetCore.OData v8.0.0-preview3 .NET 5.0

1
  • Map reference type as nullable is by design for odata. You can call the "IsRequired()" fluent API on the property when build the model to make the property as non-nullable.
    – Sam Xu
    Feb 1, 2021 at 21:52

1 Answer 1

1

I did some investigation into the differences between EF Core and OData when building a model for this simple type.

Note for clarity: The problem is more precisely with NonNullable reference (and navigation) types.

It turns out that the OData EDM builder does not explicitly handle this case and as Sam commented, that is "By Design". If you want to share common source models between EF and OData you should use the [Required] attribute. Or modify the model using the fluent API.

Update: Fixed in Microsoft.OData.ModelBuilder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.