OData / OData/AspNetCoreOData

Fluent configuration entity.Property(e => e.Name).IsRequired() don't generate Nullable="false" in Edm model but [Required] attribute do

Open
#336 1 comment 0 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Oct 19, 2021.

followup
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Hi,

It seems like fluent configuration and "IsRequired()" of entities don't generate correct metadata regarding Nullable.
Configuration:

public partial class CustomerConfiguration : IEntityTypeConfiguration<Customer>
{
	public void Configure(EntityTypeBuilder<Customer> entity)
	{
// other config...
		entity.Property(e => e.Name)
			.IsRequired()
			.HasMaxLength(255);
	}
}

Metadata:

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<edmx:DataServices>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="MyNS">
<EntityType Name="Customer">
...
<Property Name="Name" Type="Edm.String"/>
</EntityType>
</Schema>
...
</edmx:DataServices>
</edmx:Edmx>

Addin the [Required] attribute on the model will generate correct metadata.
Model:

public partial class Customer
{
// other properties...
	[Required]
	public string Name { get; set; }
}

Metadata:

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<edmx:DataServices>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="MyNS">
<EntityType Name="Customer">
...
<Property Name="Name" Type="Edm.String" Nullable="false"/>
</EntityType>
</Schema>
...
</edmx:DataServices>
</edmx:Edmx>

Please advice how I can avoid having duplicate configuration (fluent, attribute).

Platform

  • .net5
  • Microsoft.AspNetCore.OData 8.0.3
  • Microsoft.EntityFrameworkCore 5.0.10

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.