I\'m in the midst of upgrading from v1-3 to v4, but I\'ve run into a few problems.
My understanding is that DateTime is unsupported, and I have to always use DateTim
One option is to define a new property in the entity. Say Title is mapped to EF:
public partial class Title
{
public int Id { get; set; }
public string Name { get; set; }
public Nullable CreatedOn { get; set; }
}
then add a new property of DateTimeOffset:
public partial class Title
{
[NotMapped]
public DateTimeOffset? EdmCreatedOn
{
// Assume the CreateOn property stores UTC time.
get
{
return CreatedOn.HasValue ? new DateTimeOffset(CreatedOn.Value, TimeSpan.FromHours(0)) : (DateTimeOffset?)null;
}
set
{
CreatedOn = value.HasValue ? value.Value.UtcDateTime : (DateTime?)null;
}
}
}
and the code for generate OData Model looks like:
public static IEdmModel GetModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
EntityTypeConfiguration titleType= builder.EntityType();
titleType.Ignore(t => t.CreatedOn);
titleType.Property(t => t.EdmCreatedOn).Name = "CreatedOn";
builder.EntitySet("Titles");
builder.Namespace = typeof(Title).Namespace;
return builder.GetEdmModel();
}
}
The controller looks like:
public class TitlesController : ODataController
{
CustomerManagementSystemEntities entities = new CustomerManagementSystemEntities();
[EnableQuery(PageSize = 10, MaxExpansionDepth = 5)]
public IHttpActionResult Get()
{
IQueryable titles = entities.Titles;
return Ok(titles);
}
public IHttpActionResult Post(Title title)
{
entities.Titles.Add(title);
return Created(title);
}
}