When trying to execute a .Where() on my database context from a model, I am hit with this error message:
System.Data.Entity<RPSManagementSystem.Model.StoreUser> does not contain a definition for Where...
This works when calling it from a controller. What gives?
From the model:
[NotMapped]
private List<StoreUser> _stores { get; set; }
[NotMapped]
public List<StoreUser> Stores
{
get
{
if (this._stores == null || this._stores.Count <= 0)
{
using (RPSEntities db = new RPSEntities())
{
this._stores = db.StoreUsers.Where(su => su.Username == this.Username);
}
}
return _stores;
}
}
Just to make sure I'm not crazy, I pasted this into my controller - where it looks to be working. Screenshots below:
in the model:
in the controller:
A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext. Set method.
Intuitively, a DbContext corresponds to your database (or a collection of tables and views in your database) whereas a DbSet corresponds to a table or view in your database.
In Entity Framework Core, the DbSet represents the set of entities. In a database, a group of similar entities is called an Entity Set. The DbSet enables the user to perform various operations like add, remove, update, etc. on the entity set.
A DbSet represents an entity set. An entity set is defined as a set of entities of the same entity type. From the perspective of the database, it usually represents the table. Each Entity type must expose the DbSet Property to be able to participate in the CRUD Operations.
But there is no method called FromSql on DBSet. How can i execute raw queries? Any idea? Thanks. The general EF Core docs, which you should go through, are here. If you want to use raw SQL to query entities which are mapped in your context, read this.
The results of a LINQ query against a DbSet will contain the results returned from the database and may not reflect changes made in the context that have not been persisted to the database. For example, the results will not contain newly added entities and may still contain entities that are marked for deletion.
This implementation always throws an exception as binding directly to a DbSet will result in a query being sent to the database every time the data binding framework requests the contents of the collection.
DbSet<TEntity> objects are usually obtained from a DbSet<TEntity> property on a derived DbContext or from the Set<TEntity> () method. public abstract class DbSet < TEntity > : Microsoft.
Add using System.Linq;
in your model class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With