为了解决这个问题,我在创建后将所有属性设置为默认值:
public void InitializeCreatedEntity(object entity)
{
Type tipoObj = entity.GetType();
string nomeEntidade = GetNomeEntidade(entity);
foreach (EntityType entityType in EntityTypes)
{
if (entityType.Name.Equals(nomeEntidade))
{
foreach (EdmProperty property in entityType.Properties)
{
if (!property.Nullable)
{
PropertyInfo info = tipoObj.GetProperty(property.Name);
switch (property.TypeUsage.EdmType.Name)
{
case "String": info.SetValue(entity, "", null);
break;
case "Int32": info.SetValue(entity, DefaultInt, null);
break;
case "Int64": info.SetValue(entity, DefaultInt, null);
break;
case "Int16": info.SetValue(entity, DefaultInt, null);
break;
case "Binary": Byte[] initByteArray = { };
info.SetValue(entity, initByteArray, null);
break;
case "DateTime": info.SetValue(entity, DateTime.Now, null);
break;
}
}
}
}
}
}