Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBSet does not contain a definition for Where [duplicate]

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:

enter image description here

in the controller:

enter image description here

like image 336
drewwyatt Avatar asked Nov 10 '14 16:11

drewwyatt


People also ask

What does DbSet mean?

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.

What is the difference between DbSet and DbContext?

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.

What is DbSet in Entity Framework Core?

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.

What is DbSet and 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.

Is there a method called fromsql on dbset?

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.

What are the results of a LINQ query against a dbset?

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.

Why does this implementation throw an exception when binding to dbset?

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.

How do I get the tentity of a dbset?

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.


Video Answer


1 Answers

Add using System.Linq; in your model class

like image 74
Alberto Avatar answered Oct 11 '22 14:10

Alberto