-1

I have DbSets that correspond to tables in a database using Entity Framework (code-first). I have some data that doesn't need to be stored in the database, but it makes sense to access it via the DbContext instance as that is how the program has been created.

    public DbSet<z> name0 { get; set; }
    public DbSet<y> name1 { get; set; }
    public List<x> name2 { get; set; } = new List<x>()
    {
        new x()
        {...},
        new x()
        {...},
...

I want (in this example) to NOT create any table or reference to name2, but I want to refer to it via the DbContext.

(Yes I know DbContext implies it is stored in the database, I am using the class to store "data", so whether that's externally supplied or through the application itself)

5
  • A DbSet always represents a database table or view. If you define a DbSet the table will be created. Maybe use something like this: stackoverflow.com/a/45485220/13574233 Commented Nov 10, 2021 at 10:29
  • Currently I have List<>s defined which didn't seem to stop EF from producing tables; I just want to define a collection of objects without creating a table and return instances of an object directly from the app without pulling from a database to improve performance. Commented Nov 10, 2021 at 10:43
  • I would say that polluting your context with lists is a bad idea to start with, though I am surprised that EF converts them into tables. I suppose you could configure the entities to be ignored, so for a List<YourType> you would do builder.Entity<YourType>().Ignore() Commented Nov 10, 2021 at 10:56
  • Thanks for the suggestion, I'm currently considering creating a wrapper as something like "AppData" So I can return either DbSets or Lists based on TEntity, something like that. Creating abstract implementations like this is still somewhat new to me but I imagine this is the correct way to go about this. Commented Nov 10, 2021 at 13:50
  • What's the question here? EF does not create tables for these lists. What makes you conclude that EF does? Commented Apr 28 at 10:55

1 Answer 1

0
public class AppDbContext : DbContext
{
    // No mapping to a table
    public DbSet<Prodct> Products { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>().ToTable(null); // or modelBuilder.Entity<Product>().Ignore();
        // other configurations
    }
}


public class Product
{
    public int Id { get; set; }
    public string Title { get; set; }
    // ... other properties
}
Sign up to request clarification or add additional context in comments.

2 Comments

How does this answer the question? They're not looking for unmapped DbSets.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.