gpt4 book ai didi

entity-framework - EF6 中是否仍然支持模型定义的函数?

转载 作者:行者123 更新时间:2023-12-02 01:39:00 35 4
gpt4 key购买 nike

模型定义的函数在这里讨论:

  • 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 中的 元素的位置,因为它没有将其放入 EdmModel (EdmModel.AddItem( EdmFunction)没有被调用)

    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.

    最佳答案

    看来,模型定义的函数首先可用于代码。这是您的 ParseInt 的版本例子 :

    namespace EfTestModelFunctions
    {
    public class CustomFunctionConvention : IConceptualModelConvention<EdmModel>
    {
    public void Apply(EdmModel item, DbModel model)
    {
    var functionParseInt = new EdmFunctionPayload()
    {
    CommandText = String.Format("CAST(strValue AS {0})", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
    Parameters = new[] {
    FunctionParameter.Create("strValue", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String), ParameterMode.In),
    },

    ReturnParameters = new[] {
    FunctionParameter.Create("ReturnValue", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32), ParameterMode.ReturnValue),
    },
    IsComposable = true
    };

    var function = EdmFunction.Create("ParseInt", model.ConceptualModel.EntityTypes.First().NamespaceName, DataSpace.CSpace, functionParseInt, null);
    model.ConceptualModel.AddItem(function);
    }
    }

    public class RootDataContext : DbContext
    {
    public RootDataContext()
    : base("Data Source=******")
    {
    Database.SetInitializer(new NullDatabaseInitializer<RootDataContext>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    base.OnModelCreating(modelBuilder);
    modelBuilder.Conventions.Add<CustomFunctionConvention>();
    }

    public DbSet<RootEntity> Roots { get; set; }

    // declare the function with the Context's NameSpace
    [DbFunction("EfTestModelFunctions", "ParseInt")]
    public static int ParseInt(string value)
    {
    throw new NotImplementedException();
    }
    }
    }

    用法将是:
    var query = ctx.Roots.Where(r => RootDataContext.ParseInt(r.StringProperty)==123);

    此外,在涉及数据库迁移/初始化时似乎存在问题。看
    UpForGrabs: Unblock creation of model defined functions in model conventions

    关于entity-framework - EF6 中是否仍然支持模型定义的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29503962/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com