我有这堂课:
公共部分类 VehicleSize { public VehicleSize() { }
public System.Guid VehicleSizeId { get; set; }
public int Title { get; set; }
public int SizeOrder { get; set; }
public System.DateTime DateCreated { get; set; }
public Nullable<System.DateTime> DateDeleted { get; set; }
}
我正在使用 BreezeJS。
Nullable 的处理似乎有点奇怪。该字段是可选的。但它一直抱怨,直到我从 javascript 中明确设置 DateDeleted = null 。如果我不将该字段设置为 null,我将收到“保存 TypeError 的问题:entity.dateDeleted 为 null”。
如果我在 javascript 中检查 .dateDeleted 属性,它似乎是一个 javascript Date 对象,但 getTime 是 NAN。在这种情况下,它不应该为空,并且 BreezeJS 验证器跳过验证它,因为该字段为空。
来自服务器的元数据将字段定义为:
“名称”:“日期删除”,“类型”:“Edm.DateTime”,“可为空”:“真”
有人知道我如何创建可选的 DateTime 字段吗?
在我原来的帖子之后。该问题似乎与将实体从一个实体管理器导出到另一个实体管理器有关。以“null”开头的日期字段在再次导入后变为“无效日期”,如下代码所示:
var vehicleSizeId = 'E9DA5803-BB65-4751-AA22-17B54A1EE7C1';
alert('getting vehicle from db');
var query = breeze.EntityQuery
.from("VehicleSizes")
.where("vehicleSizeId", "==", vehicleSizeId);
var em = new breeze.EntityManager("api/Todo");
em.enableSaveQueuing(true);
var sandBoxEm = em.createEmptyCopy();
sandBoxEm.enableSaveQueuing(true);
em.executeQuery(query)
.then(function (data) {
alert('Found the vehicle size in the original entity manager')
var entity = data.results[0];
alert('Org Date Deleted == ' + entity.dateDeleted); // Its null at this point
var bundle = em.exportEntities();
sandBoxEm.importEntities(bundle, { mergeStrategy: breeze.MergeStrategy.OverwriteChanges });
});
sandBoxEm.executeQuery(query)
.then(function (data) {
alert('Found the vehicle size')
var entity = data.results[0];
alert('Date Deleted == ' + entity.dateDeleted); // Now its invalid date
entity.sizeOrder = entity.sizeOrder + 1;
entity.titleTranslation.text = entity.titleTranslation.text + "_x";
//entity.titleTranslation.dateDeleted = null;
//entity.dateDeleted = null;
try {
sandBoxEm.saveChanges().then(
function () {
alert('It saved ok');
}).fail(
function (error) {
var firstItem = error.entitiesWithErrors[0];
var firstError = firstItem.entityAspect.getValidationErrors()[0];
var msg = "prop: " + firstError.property.parentType.name + " = " + firstError.errorMessage;
alert(msg);
}
);
}
catch (ex) {
alert( "Problem saving " + ex );
}
}
).fail(
function () {
alert('Getting the vehicle size failed');
});