EF用户定义的功能迁移

发布于 2025-01-25 18:06:56 字数 2092 浏览 0 评论 0原文

我正在尝试使用实体框架添加用户定义的功能。

我已经定义了它们如下:

public IQueryable<GetProjectInspectionsFunctionResult> GetProjectInspections(int clientId, int projectId) => FromExpression(() => GetProjectInspections(clientId, projectId));
public IQueryable<GetProjectItemsFunctionResult> GetProjectItems(int clientId, int projectId) => FromExpression(() => GetProjectItems(clientId, projectId));
public IQueryable<GetProjectLopItemsFunctionResult> GetProjectLopItems(int clientId, int projectId) => FromExpression(() => GetProjectLopItems(clientId, projectId));
public IQueryable<GetProjectAttachmentsFunctionResult> GetProjectAttachments(int clientId, int projectId) => FromExpression(() => GetProjectAttachments(clientId, projectId));

modelBuilder.HasDbFunction(typeof(QAppDbContext).GetMethod(nameof(GetProjectInspections))).HasName("fn_GetProjectInspections");
modelBuilder.HasDbFunction(typeof(QAppDbContext).GetMethod(nameof(GetProjectItems))).HasName("fn_GetProjectItems");
modelBuilder.HasDbFunction(typeof(QAppDbContext).GetMethod(nameof(GetProjectLopItems))).HasName("fn_GetProjectLopItems");
modelBuilder.HasDbFunction(typeof(QAppDbContext).GetMethod(nameof(GetProjectAttachments))).HasName("fn_GetProjectAttachments");

都有配置

public class GetProjectInspectionsConfiguration : IEntityTypeConfiguration<GetProjectInspectionsFunctionResult>
{
     public void Configure(EntityTypeBuilder<GetProjectInspectionsFunctionResult> builder)
     {
          builder.HasNoKey();
     }
}

我还为每个功能

migrationBuilder.Sql(
     @"CREATE FUNCTION [dbo].[fn_GetProjectInspections]
        (@clientId INT, @projectId INT)
     RETURNS TABLE
     AS
     RETURN
     (
        WITH clients AS(...)
        SELECT...
     )"
)

dbContext中, dbcontextsnapshot因为EF添加.totable(null) for for evernuction -cartion类型(例如 getProjectInSpectionsFunctionFunctionFunctionResult )。

有没有办法克服这种情况?我认为我需要避免任何类型的注释(表,功能等)?

I'm trying to add user-defined functions with Entity Framework.

In DbContext I've defined them as follows:

public IQueryable<GetProjectInspectionsFunctionResult> GetProjectInspections(int clientId, int projectId) => FromExpression(() => GetProjectInspections(clientId, projectId));
public IQueryable<GetProjectItemsFunctionResult> GetProjectItems(int clientId, int projectId) => FromExpression(() => GetProjectItems(clientId, projectId));
public IQueryable<GetProjectLopItemsFunctionResult> GetProjectLopItems(int clientId, int projectId) => FromExpression(() => GetProjectLopItems(clientId, projectId));
public IQueryable<GetProjectAttachmentsFunctionResult> GetProjectAttachments(int clientId, int projectId) => FromExpression(() => GetProjectAttachments(clientId, projectId));

modelBuilder.HasDbFunction(typeof(QAppDbContext).GetMethod(nameof(GetProjectInspections))).HasName("fn_GetProjectInspections");
modelBuilder.HasDbFunction(typeof(QAppDbContext).GetMethod(nameof(GetProjectItems))).HasName("fn_GetProjectItems");
modelBuilder.HasDbFunction(typeof(QAppDbContext).GetMethod(nameof(GetProjectLopItems))).HasName("fn_GetProjectLopItems");
modelBuilder.HasDbFunction(typeof(QAppDbContext).GetMethod(nameof(GetProjectAttachments))).HasName("fn_GetProjectAttachments");

I also have a configuration for each function:

public class GetProjectInspectionsConfiguration : IEntityTypeConfiguration<GetProjectInspectionsFunctionResult>
{
     public void Configure(EntityTypeBuilder<GetProjectInspectionsFunctionResult> builder)
     {
          builder.HasNoKey();
     }
}

I've also created an empty migration and defined each function:

migrationBuilder.Sql(
     @"CREATE FUNCTION [dbo].[fn_GetProjectInspections]
        (@clientId INT, @projectId INT)
     RETURNS TABLE
     AS
     RETURN
     (
        WITH clients AS(...)
        SELECT...
     )"
)

And after I run the migration, an error occurs in DbContextSnapshot because EF adds .ToTable(null) for each function result type (eg. GetProjectInspectionsFunctionResult).

Is there a way to overcome this situation? I think that I need to avoid any type of annotation (table, function, etc.)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2025-02-01 18:06:56

我通过明确指定每个功能结果实体的名称(由方法过载引起)来解决问题:

modelBuilder.Entity<GetProjectInspectionsFunctionResult>((x) =>
{
    x.HasNoKey();
    x.ToTable(name: null);
})

I managed to fix the issue by specifying the name explicitly for each function result entity (caused by methods overload):

modelBuilder.Entity<GetProjectInspectionsFunctionResult>((x) =>
{
    x.HasNoKey();
    x.ToTable(name: null);
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文