简体   繁体   中英

Same db context multiple ToTable

I have a product configuration as such:

internal sealed class ProductConfiguration : EntityTypeConfiguration<Product>
{
    /// <summary>
    /// Initializes a new instance of the <see cref="ProductConfiguration"/> class.
    /// </summary>
    public ProductConfiguration()
        : base()
    {
        // Configure Properties
        HasKey(n => new { n.ID });

        // Specify the name of the table to get the data from
        ToTable("vw_Products");
    }
}

Which I can then get using the context

public IDbSet<Product> Products { get; set; }

Which all works well. However, I have a part of the site that is using a filtered set of the products and it is faster to use a new view to filter the products rather than filtering them with entity framework (under 1 second instead of 5 seconds using a linq query in ef)

Is there a way I can change the ToTable value only for a certain instance of trying to get a filtered context without having to create a new entity.

So for example, where I get the products -

context.Products

Can I change the table for this to look at the new view?

I have tried copying the configuration to look at the new view and then added a new dbset as such:

public IDbSet<Product> DesignerProducts { get; set; }

But this doesn't seem to work (unless there is a way to point the configuration at that particular dbset?)

So I guess my question is, is there a way to point the configuration at a particular dbset (if the entity is the same) or is there a way to point the context at a particular view when it is queried

In effect I am wanting a context with the following 2 dbsets:

public IDbSet<Product> Products { get; set; }
public IDbSet<Product> DesignerProducts { get; set; }

Ok, I don't think it is possible to achieve what I want so in order to get around it I have had to make a new context and put my new filtered list into that instead. If anyone can think of a better way to do this then please feel free to post an answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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