Setting a data type in ODataConventionModelBuilder

I saw in the documentation ODatathat there are Edmtypes Dateand Time. Currently, there are many fields in my database that are Daterepresented in EF as DateTimes, so it ODataConventionModelBuilderdeclares them as Edm.DateTime. How to change them to Edm.Date?

I was hoping I could do this:

entityType.Property(p => p.AgreementDate).EdmType = EdmType.Date;
+4
source share
2 answers

The corresponding Edm type of a particular property is mapped from the CLR type and cannot be overridden ODataConventionModelBuilder.

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

Edm, GetEdmModel ODataConventionModelBuilder, Edm.Date Edm, API OData.

+2

, - , . API , .

EDM, , ODataConventionModelBuilder, date:

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

:

 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)));

.

+1

Source: https://habr.com/ru/post/1544121/


All Articles