简体   繁体   English

BreezeJS + EF Core + 表值函数的问题

[英]Problems with BreezeJS + EF Core + Table Valued Functions

I have a TVF (LoadUserInfo) in the DB that is being wired up to return data as per a class (UserInfo) in my .net project.我在数据库中有一个 TVF (LoadUserInfo),它被连接以根据我的 .net 项目中的类 (UserInfo) 返回数据。

I am defining an IQueryable as follows:我正在定义一个 IQueryable 如下:

 public IQueryable<UserInfo> LoadUserInfo(Guid? userId) 
    => FromExpression(() => LoadUserInfo(userId));

And then for EFCore in my OnModelCreating I setup as follows:然后对于我的 OnModelCreating 中的 EFCore,我设置如下:

modelBuilder.HasDbFunction(typeof(AppDatabase).GetMethod(nameof(LoadUserInfo))!);

Now in my BreezeController I can easily return info directly from my TVF现在在我的 BreezeController 中,我可以轻松地直接从我的 TVF 返回信息

  public IEnumerable<ReportInfo> UserInfos() {
    return PersistenceManager.Context.LoadUserInfo(User.GetUserId());
  }

The above works fine in terms of returning data from the API and also all metadata is correctly generated - ie even my UserInfo class is generated for use on the client side.以上在从 API 返回数据和正确生成所有元数据方面工作正常 - 即甚至生成我的 UserInfo 类以供在客户端使用。

The problem is that when the metadata is returned during the API call it does NOT include typeinformation for UserInfo, it ignores it completely.问题是在 API 调用期间返回元数据时,它不包含 UserInfo 的类型信息,它会完全忽略它。 I realized this was probably because I never declared UserInfo as a DbSet in the dbContext for EF Core.我意识到这可能是因为我从未在 EF Core 的 dbContext 中将 UserInfo 声明为 DbSet。 I went ahead and did that...我继续这样做......

public DbSet<UserInfo> UserInfos => Set<UserInfo>();

Now UserInfos is included in the metadata but now when the server tries to return the data from the Breezecontroller it fails with the following message:现在 UserInfos 包含在元数据中,但现在当服务器尝试从 Breezecontroller 返回数据时,它会失败并显示以下消息:

Invalid column name 'PeriodEnd'.
Invalid column name 'PeriodStart'.

Those are standard built in EF Core columns and I am suspecting that since UserInfo is now declared in the dbSet its expecting them to be there.这些是 EF Core 列中内置的标准,我怀疑由于 UserInfo 现在已在 dbSet 中声明,因此它期望它们存在。 But given that its not a real table, its just a class wired up to a TVF, they don't exist and its failing.但是考虑到它不是一个真正的表,它只是一个连接到 TVF 的类,它们不存在并且它失败了。

What is the correct way to set this up so that it doesn't fail?设置它以使其不会失败的正确方法是什么? Thanks谢谢

I think you can solve this on the Breeze client.我认为您可以在 Breeze 客户端上解决此问题。 You don't need to add that DbSet .您不需要添加该DbSet

Although the entity type is not returned in the query results, you can tell the Breeze client what type to expect by using .toType in your query:尽管查询结果中没有返回实体类型,但您可以通过在查询中使用.toType来告诉 Breeze 客户端预期的类型:

var query = EntityQuery.from('UserInfos').toType('UserInfo');

Alternatively, you can patch the metadata (on the client) to tell Breeze what entity type to expect from that endpoint:或者,您可以修补元数据(在客户端)以告诉 Breeze 从该端点期望什么实体类型:

var meta = manager.metadataStore;
meta.setEntityTypeForResourceName('UserInfos', 'UserInfo');

See the Breeze docs for more info.有关更多信息,请参阅Breeze 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM