【发布时间】:2015-06-12 18:43:34
【问题描述】:
此处讨论模型定义的函数:
- https://msdn.microsoft.com/en-us/library/vstudio/dd456857(v=vs.110).aspx
- https://msdn.microsoft.com/en-us/library/dd456812.aspx
- Entity Framework 6 Code First function mapping
- http://www.c-sharpcorner.com/UploadFile/ff2f08/model-defined-function/
EF6.1.2 是否支持这些?
我正在逐步浏览 Edm/DbModel 的内容,但我终其一生都无法确定应该解析 csdl 中的
ExpressionConverter.FindFunction 在 EdmModel._functions 中查找,并且 _functions 仅由 EdmModel.AddItem(EdmFunction) 添加,并且仅由扩展方法 EdmModelExtensions.AddFunction() 调用,我在 EntityFramework 源代码中找不到任何地方调用该函数。我一定错过了一些简单的东西......
更多:我放弃了在 edmx 中定义函数,现在我正在以编程方式创建我的 EdmFunction 并将其添加到自定义 IConceptualModelConvention.Apply() 方法中:
class CustomFunctionConvention : IConceptualModelConvention<EdmModel>
{
public void Apply(EdmModel item, DbModel model)
{
var functionPayload = new EdmFunctionPayload () {
CommandText = "CAST (strValue AS int)",
Parameters = new [] {
FunctionParameter.Create("strValue", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String).GetEdmPrimitiveType(), ParameterMode.In),
},
ReturnParameters = new [] {
FunctionParameter.Create("ReturnValue", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32).GetEdmPrimitiveType(), ParameterMode.ReturnValue),
},
IsComposable = true,
};
var function = EdmFunction.Create("ParseInt", "MyNamespace", DataSpace.CSpace, functionPayload, null);
model.ConceptualModel.AddItem(function);
}
}
但现在我在 EdmItemCollection.LoadItems() 中遇到了一堆架构错误:
Schema specified is not valid. Errors:
(0,0) : error 0005: The 'Aggregate' attribute is not allowed.
(0,0) : error 0005: The 'BuiltIn' attribute is not allowed.
(0,0) : error 0005: The 'NiladicFunction' attribute is not allowed.
(0,0) : error 0005: The 'IsComposable' attribute is not allowed.
(0,0) : error 0005: The 'ParameterTypeSemantics' attribute is not allowed.
(0,0) : error 0005: The 'Schema' attribute is not allowed.
(0,0) : error 0005: The 'Mode' attribute is not allowed.
【问题讨论】:
-
也许这会有所帮助:stackoverflow.com/a/29539227/1236044
-
不完全是,那是 sql 定义的函数。我正在尝试使用模型定义的函数。不同的野兽。
-
当然,它们使用 EF 设计器工作得很好。 msdn.microsoft.com/en-us/library/vstudio/… 但我认为你的问题是代码优先,而不是数据库/模型优先。
标签: entity-framework ado.net ado.net-entity-data-model