Tim
Tim

Reputation: 3038

Setting data type in ODataConventionModelBuilder

I've seen in OData documentation that there are Edm types Date and Time. Currently in my database there are many DATE fields being represented in EF as DateTimes, so the ODataConventionModelBuilder is declaring them as Edm.DateTime. How can I change them to Edm.Date?

Was hoping I could do this:

entityType.Property(p => p.AgreementDate).EdmType = EdmType.Date;

Upvotes: 4

Views: 2081

Answers (2)

Dejan
Dejan

Reputation: 10323

Here's my answer in case someone is interested in the details of how to implement Feng Zhao's suggestion. I didn't find the API too discoverable, so I wanted to share.

First, build your EDM model as usual with the ODataConventionModelBuilder but ignore the date property:

...
entitType.Ignore(p => p.AgreementDate);
...
IEdmModel model = builder.GetEdmModel();

Then add the date property "manually":

 var myType = (EdmStructuredType)model.FindDeclaredType(typeof(MyType).FullName);
 var dateType = (IEdmPrimitiveType)model.FindType("Edm.Date");
 myType.AddProperty(new EdmStructuralProperty(myType, "AgreementDate", new EdmPrimitiveTypeReference(dateType, false)));

That's it.

Upvotes: 1

Feng Zhao
Feng Zhao

Reputation: 2995

The corresponding Edm type of certain property is mapped from the CLR type and can't be overrided by ODataConventionModelBuilder.

If you want to change the Edm type, you can ignore these properties within the ODataConventionModelBuilder.

After getting the Edm model by calling GetEdmModel on the ODataConventionModelBuilder, you can add these properties with Edm.Date to the Edm model by calling OData APIs.

Upvotes: 2

Related Questions