EF用户定义的功能迁移
我正在尝试使用实体框架添加用户定义的功能。
我已经定义了它们如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过明确指定每个功能结果实体的名称(由方法过载引起)来解决问题:
I managed to fix the issue by specifying the name explicitly for each function result entity (caused by methods overload):