entity framework core - Cannot create a DbSet for 'Product' because this type is not included in the model for t
- c - Solaris 10 make Error code 1 Fatal Error when trying to build python 2.7.16 - Stack Overflow 推荐度:
- javascript - How to dismiss a phonegap notification programmatically - Stack Overflow 推荐度:
- javascript - Get the JSON objects that are not present in another array - Stack Overflow 推荐度:
- javascript - VS 2015 Angular 2 import modules cannot be resolved - Stack Overflow 推荐度:
- javascript - Type 'undefined' is not assignable to type 'menuItemProps[]' - Stack Overflow 推荐度:
- 相关推荐
I'm trying to unit test the derived class of a dbContext. I keep getting the error message
[Fact]
public void GetAssync_Method_Called()
{
var options = new DbContextOptionsBuilder<CoreDbContext>()
//.UseModel() USE MODEL
.UseInMemoryDatabase(databaseName: "test")
.Options;
using (var context = new CoreDbContext(options))
{
context.Set<Product>(); //THIS IS THE WAY TO ADD AN ENTITY IN THE CONTEXT...????
Expression<Func<Product, bool>> filter = x => !x.IsDeleted == 1;
var result = GetAllAsNoTrackin(filter);
Assert.Equal(1, result.Count());
}
}
Below are the CoreDbContext and the method I'm testing.
public class CoreDbContext : DbContext
{
public CoreDbContext(DbContextOptions<CoreDbContext> options) : base(options)
{
}
public IQueryable<Product> GetAllAsNoTrackin(Expression<Func<Product, bool>> filter)
{
var dbSet = Set<Product>().AsNoTracking(); //GETTING ERROR HERE...
//more code...
}
}
I keep getting the same error:
Cannot create a DbSet for 'Product' because this type is not included in the model for the context
After googling, it looks like DbContextOptionsBuilder
has a method called UseModel(IModel model)
that can be called instead of the OnModelCreating(ModelBuilder builder)
.
Unfortunately, I didn't see anywhere any code on how to use it. Also, adding a new entity is happening after the context has been create, while UseModel()
is an extension method of the options to be passed to the DbContext
.
Thanks for helping
I'm trying to unit test the derived class of a dbContext. I keep getting the error message
[Fact]
public void GetAssync_Method_Called()
{
var options = new DbContextOptionsBuilder<CoreDbContext>()
//.UseModel() USE MODEL
.UseInMemoryDatabase(databaseName: "test")
.Options;
using (var context = new CoreDbContext(options))
{
context.Set<Product>(); //THIS IS THE WAY TO ADD AN ENTITY IN THE CONTEXT...????
Expression<Func<Product, bool>> filter = x => !x.IsDeleted == 1;
var result = GetAllAsNoTrackin(filter);
Assert.Equal(1, result.Count());
}
}
Below are the CoreDbContext and the method I'm testing.
public class CoreDbContext : DbContext
{
public CoreDbContext(DbContextOptions<CoreDbContext> options) : base(options)
{
}
public IQueryable<Product> GetAllAsNoTrackin(Expression<Func<Product, bool>> filter)
{
var dbSet = Set<Product>().AsNoTracking(); //GETTING ERROR HERE...
//more code...
}
}
I keep getting the same error:
Cannot create a DbSet for 'Product' because this type is not included in the model for the context
After googling, it looks like DbContextOptionsBuilder
has a method called UseModel(IModel model)
that can be called instead of the OnModelCreating(ModelBuilder builder)
.
Unfortunately, I didn't see anywhere any code on how to use it. Also, adding a new entity is happening after the context has been create, while UseModel()
is an extension method of the options to be passed to the DbContext
.
Thanks for helping
Share Improve this question edited Nov 15, 2024 at 21:41 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Nov 15, 2024 at 21:23 Richard77Richard77 21.7k52 gold badges165 silver badges273 bronze badges1 Answer
Reset to default 1A DbContext
needs to know about entities, it doesn't discover and assume every class you define in a project is an entity. There are a few ways this can be done via convention and/or by explicit configuration.
The most common way a DbContext knows about entities is through declared DbSets:
public class CoreDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
Here the DbContext initialization will be aware of a Product entity and use conventions to marry it, and its public properties, including navigation property entity types, to tables.
Where you do not have a DbSet
or relation to an entity with a DbSet
, or convention cannot be followed to map it correctly, you need explicit configuration. The two common approaches are through OnModelCreating
or using an IEntityTypeConfiguration
In OnModelCreating()
you would declare entities:
modelBuilder.Entity<Product>();
That tells the DbContext at the minimum to expect a Product entity. Everything will be resolved by convention or attributes declared in the entity class.
Using IEntityTypeConfiguration<TEntity>
involves creating classes that implement this interface to provide the configuration for that entity model. For larger projects this helps anize entity configuration rather than having a huge OnModelCreating()
full of configurations for all entities in the system. Once these are defined they can be registered with the model builder using modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);
within the OnModelCreating
. This assumes the entities are declared in the same assembly as the DbContext
, otherwise you can pass whatever assembly reference(s) to locate the IEntityTypeConfiguration
implementations for each entity. A DbContext
can access configured entities via Set<TEntity>
but it's generally simpler to declare and access these entities through DbSet<TEnitity>
properties in the DbContext.
Pretty much everything to do with accessing and configuring entities and DbContext
s is covered in depth with examples in Microsoft's documentation: https://learn.microsoft/en-us/ef/core/
- 百度部分下线盗版视频 反盗版方敦促其全面停止
- 微软押注Win8平板最危险因素是硬件商
- java - Difficulty Embedding ICC Profile into PDF Using PDFBox, iText, and Ghostscrip - Stack Overflow
- android - Segmentation Fault at start up of Maui App - Stack Overflow
- maven - How can I get the dependencies about the master code of mule? - Stack Overflow
- ios - CocoaPods could not find compatible versions for pod "FirebaseCore": - Stack Overflow
- C equivalent of C++ inline - Stack Overflow
- python - Why is my KivyMD Button not changing Color with "md_bg_color"? - Stack Overflow
- sharepoint - How to copy site page from one site to another site using Power Automate? - Stack Overflow
- swiftui - Swift Mocking a throwing function - Stack Overflow
- javascript - Why does every object in BabylonJS require a name? - Stack Overflow
- postgresql - Postgres Postgis ST_DWithin query is not accurate - Stack Overflow
- android - How to preload Initial data earlier in Jetpack Paging with Pager and PagingConfig? - Stack Overflow
- node.js - How to extract frames in sequence as PNG images from ffmpeg stream? - Stack Overflow
- volttron - POSTMAN REST Call Results in RPC Timed Out: 5 Seconds - Stack Overflow
- python - Call function from macos framework (e.g. IntelPowerGadget) - Stack Overflow
- Page Performance Issue with Countdown in Angular on Component Reload - Stack Overflow