A temporary (but slightly dodgy) workaroundfor the TimeStamp properties is as follows.
However, I can't see any way of accessing the fact it is specifically a TimeStamp field (this example is assuming all Byte[] fields are timestamps), and it doesn't even touch on the Default problem above.
I could have used the field name "ts" for all timestamps, but the ideal would be to know the underlying database type.
public class QsEntitySaveInterceptor : EntityServerSaveInterceptor
{
protected override bool ValidateSave()
{
var now = DateTime.Now;
var e = base.EntityManager.FindEntities(EntityState.Added).Cast<Entity>().ToList();
foreach(var entity in e)
{
var timestamps = (entity).EntityAspect.EntityMetadata.EntityProperties.Where(ep => ep.DataType == typeof(byte[]));
foreach (var prop in timestamps)
prop.PropertyInfo.SetValue(entity, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, null);
var createddates = (entity).EntityAspect.EntityMetadata.EntityProperties.Where(ep => ep.Name == "DateCreated" || ep.Name == "LastUpdated");
foreach (var prop in createddates)
prop.PropertyInfo.SetValue(entity, now, null);
}
return base.ValidateSave();
}
}So... interesting questions:
- is there any way to find the database type that underlies an Entity
property?
- is it right that DateTime fields on new Entities are always set to Today? This might be dangerous; forgetting to set it in code might not be detected, and what if we want to know in the Save Interceptor whether a value was entered or not? Might a date of DateTime.Min (or 01-01-1900 for non-DateTime2) not be better?
Also, non-nullable string fields don't have a default of String.Empty, so why are DateTimes set to a value?
- do you know of any way to make the EF4 designer retrieve other SQL metadata such as 'Default Value or Binding'?
- is it possible to influence the SQL of fields that will be inserted on added entities (i.e. remove inserted parameters so defaults are used)?
Ah, so many questions and so little time to answer them all...
Edited by jsobell - 24-Jun-2010 at 6:09pm