简体   繁体   English

实体框架核心-一种用于多个查询的模型

[英]Entity Framework Core - one model for multiple queries

I have a .NET Core API using Entity Framework Core. 我有一个使用实体框架核心的.NET Core API。 We use .FromSQL() to execute stored procedures. 我们使用.FromSQL()执行存储过程。 The problem I'm having is we have two queries that return Account information - one for a single account and one for a paged list of accounts. 我遇到的问题是,我们有两个查询返回Account信息-一个查询单个帐户,另一个查询页面列表。

The account list returns a column for RowCount which the single account query does not return, so now the single account query returns an error ( Rowcount returns the total number of accounts in the database, not just the accounts on the requested page so you know how many pages of data there are). 帐户列表返回RowCount的列,单个帐户查询不返回该列,因此现在单个帐户查询返回错误( Rowcount返回数据库中帐户的总数,而不仅仅是请求页面上的帐户,因此您知道如何有很多页的数据)。

The error: 错误:

The required column 'RowCount' was not present in the results of a 'FromSql' operation 'FromSql'操作的结果中没有所需的列'RowCount'

Here is the model 这是模型

public class Account
{
    [Key]
    public long AccountId { get; set; }

    public string AccountName { get; set; }
    public bool IsDeleted { get; set; }
    public int RowCount { get; set; }

    [NotMapped]
    public bool Found => AccountId > 0;
}

And our dbContext 还有我们的dbContext

public class AccountDbContext : DbContext
{
    private DbSet<Account> TAccount { get; set; }

    public AccountDbContext(DbContextOptions<AccountDbContext> options) : base(options)
    {
    }

    public Account GetAccountById(long accountId)
    {
        var pAccount = new SqlParameter("accountId", accountId);
        Account account = TAccount.FromSql("EXEC dbo.SPR_Account_GetById @accountId", pAccount).FirstOrDefault();
        return account;
    }

    public List<Account> GetAccounts(int offset, int limit)
    {
        var pOffset = new SqlParameter("offset", offset);
        var pLimit = new SqlParameter("limit", limit);

        var dataSet = TAccount.FromSql($"EXEC dbo.SPR_Account_GetAll @offset=@offset,@limit=@limit", pOffset, pLimit).ToList();

        return dataSet;
    }
}

My question: is there a way to reuse this model for both queries? 我的问题:是否有办法在两个查询中重用此模型? I think it's dumb to create a second model with identical properties. 我认为创建具有相同属性的第二个模型是愚蠢的。 And I can never get entity to play nice with inheritance. 而且我永远也无法让实体与继承融为一体。 It always gives me an error about Discrimitator not being found. 它总是给我一个关于找不到Discrimitator的错误。

Thanks to Chris Pratt and Dennis1679, I looked further into inheritance. 多亏克里斯·普拉特(Chris Pratt)和丹尼斯(Dennis)1679,我才进一步研究了继承。 Entity Framework doesn't like it when both a base class and derived class are registered as queryable types. 当基类和派生类都注册为可查询类型时,实体框架不喜欢它。 I created a base class with the common properties and two derived classes: one for the single account and one for the list that includes the RowCount property. 我创建了具有公共属性和两个派生类的基类:一个用于单个帐户,一个用于包含RowCount属性的列表。 I'm not 100% happy with the fake inheritance, but I don't have to return a dummy RowCount from the stored proc and I don't have to duplicate my object. 我对假继承不是100%满意,但是我不必从存储的proc中返回虚拟RowCount,也不必复制对象。

public class AccountBase
{
    [Key]
    public long AccountId { get; set; }

    public string AccountName { get; set; }
    public bool IsDeleted { get; set; }

    [NotMapped]
    public bool Found => AccountId > 0;
}

public class AccountDataModel : AccountBase
{
}

public class AccountCount : AccountBase
{
   public int RowCount { get; set; }
}

Then, in my dbContext: 然后,在我的dbContext中:

public class AccountDbContext : DbContext
{
    private DbSet<AccountDataModel> TAccount { get; set; }
    private DbSet<AccountCount> AccountList { get; set; }

    public AccountDbContext(DbContextOptions<AccountDbContext> options) : base(options)
    {
    }

    public Account GetAccountById(long accountId)
    {
        var pAccount = new SqlParameter("accountId", accountId);
        Account account = TAccount.FromSql("EXEC dbo.SPR_Account_GetById @accountId", pAccount).FirstOrDefault();
        return account;
    }

    public List<Account> GetAccounts(int offset, int limit)
    {
        var pOffset = new SqlParameter("offset", offset);
        var pLimit = new SqlParameter("limit", limit);

        var dataSet = AccountList.FromSql($"EXEC dbo.SPR_Account_GetAll @offset=@offset,@limit=@limit", pOffset, pLimit).ToList();

        return dataSet;
    }
}

In order to use FromSql with an entity, there must be a direct one-to-one correlation between what's returned and the properties on the entity. 为了将FromSql与实体一起使用,返回的内容与实体的属性之间必须存在直接的一对一关联。 This is not optional and there is no alternative. 这不是可选的,没有其他选择。

However, if you use a DTO class, you can do whatever you like. 但是,如果使用DTO类,则可以做任何您想做的事情。 You just add it to your context like: 您只需将其添加到您的上下文中,例如:

public DbQuery<AccountDTO> AccountDTOs { get; set; }

And then you can use FromSql on that instead, without any of the issues you're having. 然后,您可以改用FromSql ,而不会遇到任何问题。

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

相关问题 实体框架将多个查询组合为一个 - Entity Framework combining multiple queries into one 实体框架中的多个查询,而不是一个 - Multiple queries in Entity Framework instead of one Entity Framework Core:如何将 2 个 COUNT 查询合并为一个 - Entity Framework Core: How to combine 2 COUNT queries into one 用于并行运行查询的多个实体框架核心上下文的依赖注入 - Dependency injection of multiple Entity Framework Core Contexts for running queries in parallel 使用 Entity Framework Core 重用查询 - Reusing queries with Entity Framework Core 多个数据库提供者的一个实体框架模型 - One Entity Framework Model for Multiple Database Providers 一个视图中的父子模型(多个模型)(ASP.NET Core(C#)+ MVC +实体框架) - Parent-Child model (multiple model) in one view (ASP.NET Core (C#) + MVC + Entity Framework) 在 Entity Framework Core 中使用 2 个数据库共享一个 model - Using 2 databases sharing one model in Entity Framework Core Entity Framework Core:与一个基本类型表的多个关系 - Entity Framework Core: multiple relationships to one table of base type 实体框架核心数据库优先-到一个表的多个外键 - Entity Framework Core Database First - Multiple Foreign keys to one table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM