我从the Breeze documentation about BeforeSaveEntities阅读了以下内容:
“可以在此方法返回的地图中添加或删除实体”。
所以我想我可以在saveMap中添加一个新的EntityInfo实例。 我的问题是:我该怎么做?在任何地方都有这样的例子吗?
我可以完美地遍历字典。但是因为EntityInfo没有构造函数,并且它的所有字段都只是get,所以我觉得有点卡在这里。欢迎任何帮助。
由于
答案 0 :(得分:5)
好的,这是一个非常人为的 BeforeSaveEntities 覆盖示例,可以创建评论记录以及通常保存的内容。注释记录包括基于 SaveOptions.Tag 属性的值生成的注释。
protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap) {
var comment = new Comment();
var tag = ContextProvider.SaveOptions.Tag;
comment.Comment1 = (tag == null) ? "Generic comment" : tag.ToString();
comment.CreatedOn = DateTime.Now;
comment.SeqNum = 1;
var ei = ContextProvider.CreateEntityInfo(comment);
List<EntityInfo> comments;
if (!saveMap.TryGetValue(typeof(Comment), out comments)) {
comments = new List<EntityInfo>();
saveMap.Add(typeof(Comment), comments);
}
comments.Add(ei);
return saveMap;
}
}
答案 1 :(得分:1)
这个答案适用于那些选择使用objectContext而不是Code First和Nicolas使用Database First的开发人员。
我在调试中使用Breeze Source Code后发现GetEntitySetName
方法的第805行(cspaceEntityType = cspaceEntityTypes.First(et => et.FullName == entityType.FullName
)
我会收到错误“序列不包含匹配元素”
我在手表中注意到et.FullName
和entityType.FullName
没有相同的命名空间。这告诉我的comrad和我,edmx模型命名空间与对象上下文不同。
转到您的edmx模型,选择右键单击空白区域并选择属性。确保Namespace属性与Object Context相同。