-
Notifications
You must be signed in to change notification settings - Fork 21
Open
Description
The ODataConventionalModelBuilder throws an exception durng registration of entities with a key property that is of a nullable enum type:
System.ArgumentException: Type provided must be an Enum. (Parameter 'enumType')
Although key properties are never nullable in the created model, it should be possible to register key properties of a nullable enum type since there is no error for key properties of a nullable primitve type like int?
.
Assemblies affected
OData ModelBuilder 1.0.9
Reproduce steps
- Create a new .Net 6 project
- Have an entity with a property of a nullable enum type
- Create an ODataConventionalModelBuilder instance
- Register the entity type
- Create the EdmModel
Example code:
using Microsoft.OData.ModelBuilder;
using System.ComponentModel.DataAnnotations;
var builder = new ODataConventionModelBuilder();
builder.EnableLowerCamelCase();
builder.EntityType<Customer>();
var model = builder.GetEdmModel();
enum Classification
{
None,
Test
}
class Customer
{
[Key]
public Classification? Classification { get; set; }
}
Expected result
The model builder would successfully create a model with a Customer
entity that has a key property of type Classification
.
Since it is a key property, it would be marked as non-nullable in the created model.
Actual result
When creating the EdmModel the following exception occurs:
Unhandled exception. System.ArgumentException: Type provided must be an Enum. (Parameter 'enumType')
at System.RuntimeType.GetEnumUnderlyingType()
at System.Enum.GetUnderlyingType(Type enumType)
at Microsoft.OData.ModelBuilder.EnumTypeConfiguration..ctor(ODataModelBuilder builder, Type clrType)
at Microsoft.OData.ModelBuilder.ODataModelBuilder.AddEnumType(Type clrType)
at Microsoft.OData.ModelBuilder.ODataConventionModelBuilder.AddEnumType(Type type)
at Microsoft.OData.ModelBuilder.EntityTypeConfiguration.HasKey(PropertyInfo keyProperty)
at Microsoft.OData.ModelBuilder.Conventions.Attributes.KeyAttributeEdmPropertyConvention.Apply(StructuralPropertyConfiguration edmProperty, StructuralTypeConfiguration structuralTypeConfiguration, Attribute attribute, ODataConventionModelBuilder model)
at Microsoft.OData.ModelBuilder.Conventions.Attributes.AttributeEdmPropertyConvention`1.Apply(TPropertyConfiguration edmProperty, StructuralTypeConfiguration structuralTypeConfiguration, ODataConventionModelBuilder model)
at Microsoft.OData.ModelBuilder.Conventions.Attributes.AttributeEdmPropertyConvention`1.Apply(PropertyConfiguration edmProperty, StructuralTypeConfiguration structuralTypeConfiguration, ODataConventionModelBuilder model)
at Microsoft.OData.ModelBuilder.ODataConventionModelBuilder.ApplyPropertyConvention(IEdmPropertyConvention propertyConvention, StructuralTypeConfiguration edmTypeConfiguration)
at Microsoft.OData.ModelBuilder.ODataConventionModelBuilder.ApplyTypeAndPropertyConventions(StructuralTypeConfiguration edmTypeConfiguration)
at Microsoft.OData.ModelBuilder.ODataConventionModelBuilder.MapTypes()
at Microsoft.OData.ModelBuilder.ODataConventionModelBuilder.GetEdmModel()
at Program.<Main>$(String[] args) in C:\Temp\Example\Program.cs:line 8
Additional detail
- The same exception is thrown if the property is manually registered as a key on the entity type with the
HasKey
-method. - Properties of a nullable enum type that are no key can be registered on a entity type.
- The registration of key properties of a nullable primitive type like
int?
are successfull. The model contains a key property of the primitive type that is marked as non-nullable since it is a key. - After a first look into the code: There might be missing a call to
TypeHelper.GetUnderlyingType
for the case of an enum type in theHasKey
-method which is used in other code parts of the project.
Metadata
Metadata
Assignees
Labels
No labels