I am using EF 4 Database first + POCOs. Because EF has no easy way to state that incoming DateTimes are of kind UTC, I moved the property from the auto-generated file to a partial class in another file.
private DateTime _createdOn;
public virtual System.DateTime CreatedOn
{
get { return _createdOn; }
set
{
_createdOn =
(value.Kind == DateTimeKind.Unspecified)
? _createdOn = DateTime.SpecifyKind(value, DateTimeKind.Utc)
: value;
}
}
However, now every time I update the model, the automated properties get created again in the T4-generation. Of course this causes the following compilation error: "The type 'Foo' already contains a definition for 'CreatedOn'".
Is there any way to tell EF to not generate that property and to let me handle it on my own?
Update
Thanks for everyone's answers...
I created a new custom property with a different name.
public virtual System.DateTime CreatedOnUtc
{
get
{
return (CreatedOn.Kind==DateTimeKind.Unspecified)
? DateTime.SpecifyKind(CreatedOn, DateTimeKind.Utc)
: CreatedOn;
}
set
{
CreatedOn =
(value.Kind == DateTimeKind.Unspecified)
? CreatedOn = DateTime.SpecifyKind(value, DateTimeKind.Utc)
: value;
}
}
I also set all of the setters and getters of the auto-generated property to Private with the exception of those properties that I needed to use in a Linq-to-Entities query (sigh). In those cases, I set those getters to internal.
I sure wish there was a dropdown on DateTime types to specify what "Kind" of DateTime that EF should treat it as. That would have saved hours and the extra complication.
A different approach is to hook into the ObjectMaterialized event in the DbContext and set the kind there.
In my DbContext constructor, i do this:
((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += new ObjectMaterializedEventHandler(ObjectMaterialized);
and then the method looks like this:
private void ObjectMaterialized(object sender, ObjectMaterializedEventArgs e)
{
Person person = e.Entity as Person;
if (person != null) // the entity retrieved was a Person
{
if (person.BirthDate.HasValue)
{
person.BirthDate = DateTime.SpecifyKind(person.BirthDate.Value, DateTimeKind.Utc);
}
person.LastUpdatedDate = DateTime.SpecifyKind(person.LastUpdatedDate, DateTimeKind.Utc);
person.EnteredDate = DateTime.SpecifyKind(person.EnteredDate, DateTimeKind.Utc);
}
}
The downside is that you need to make sure you set it for each property that you care about but at least it gets set at the lowest possible level.
I used the same approach as Michael only then I dived a little bit deeper, and used reflection to search for DateTime and DateTime?
My solution to ensure that all the DateTime values are readed as Utc DateTimes is as followed:
First I wrote three methods which are in my DbContext Extensions method class. Because I need to use it for multiple DbContexts
public static void ReadAllDateTimeValuesAsUtc(this DbContext context)
{
((IObjectContextAdapter)context).ObjectContext.ObjectMaterialized += ReadAllDateTimeValuesAsUtc;
}
private static void ReadAllDateTimeValuesAsUtc(object sender, ObjectMaterializedEventArgs e)
{
//Extract all DateTime properties of the object type
var properties = e.Entity.GetType().GetProperties()
.Where(property => property.PropertyType == typeof (DateTime) ||
property.PropertyType == typeof (DateTime?)).ToList();
//Set all DaetTimeKinds to Utc
properties.ForEach(property => SpecifyUtcKind(property, e.Entity));
}
private static void SpecifyUtcKind(PropertyInfo property, object value)
{
//Get the datetime value
var datetime = property.GetValue(value, null);
//set DateTimeKind to Utc
if (property.PropertyType == typeof(DateTime))
{
datetime = DateTime.SpecifyKind((DateTime) datetime, DateTimeKind.Utc);
}
else if(property.PropertyType == typeof(DateTime?))
{
var nullable = (DateTime?) datetime;
if(!nullable.HasValue) return;
datetime = (DateTime?)DateTime.SpecifyKind(nullable.Value, DateTimeKind.Utc);
}
else
{
return;
}
//And set the Utc DateTime value
property.SetValue(value, datetime, null);
}
And then I go to the constructor of my WebsiteReadModelContext which is a DbContext object and call the ReadAllDateTimeValuesAsUtc method
public WebsiteReadModelContext()
{
this.ReadAllDateTimeValuesAsUtc();
}
I would use the edmx and specify another name for the CreatedOn property (such as CreatedOnInternal). Then set the access modifier for generation to Internal instead of Public. Then you can implement your custom property in the partial class and not worry about this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With