The DbSet class represents an entity set that can be used for create, read, update, and delete operations. The context class (derived from DbContext ) must include the DbSet type properties for the entities which map to database tables and views. Adds the given entity to the context with the Added state.
A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.
The DbContext class is an integral part of Entity Framework. An instance of DbContext represents a session with the database which can be used to query and save instances of your entities to a database. DbContext is a combination of the Unit Of Work and Repository patterns.
Definition. DBContext is a wrapper of ObjectContext that exposes the most commonly used features of ObjectContext. In contrast, Object Context is a class of the core Entity framework API that allows performing queries and tracking the updates made to a database using strongly typed entity classes.
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. So it makes perfect sense that you will get a combination of both!
You will be using a DbContext object to get access to your tables and views (which will be represented by DbSet's) and you will be using your DbSet's to get access, create, update, delete and modify your table data.
If you have 10 tables in your database and your application works with 5 of them (let us call them Table1 - Table 5) it would make sense to access it using a MyAppContext object where the MyAppContext class is defined thus:
public class MyAppContext : DbContext
{
public MyAppContext () : ;
public DbSet<Table1> Table1 { get; set; }
public DbSet<Table2> Table2 { get; set; }
public DbSet<Table3> Table3 { get; set; }
public DbSet<Table4> Table4 { get; set; }
public DbSet<Table5> Table5 { get; set; }
}
Note that, for instance, the identifier Table1 is used both as the name of a type and as a name of a property in the defined context type. What you see above is quite typical. An example of a class that corresponds to a table schema is given below:
public class Table1
{
public int Id {get; set;}
public string AStringField {get; set;}
//etc.
}
Have a look here for more information: http://entityframeworktutorial.net/
DbContext
generally represents a database connection and a set of tables. DbSet
is used to represent a table.
Your code sample doesn't fit the expected pattern. First, it is incomplete. Also, there are properties that really don't belong.
This pattern is more typical:
class User
{
public string IPAddress { get; set; }
public ICollection<Settings> Settings { get; set; }
public string UserName { get; set; }
}
class MyContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Settings> Settings { get; set; }
}
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