C# 关于 DbSet 和 DbContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13627829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:15:00  来源:igfitidea点击:

About DbSet and DbContext

c#entity-framework

提问by

I saw a piece of code that mixes DbSetand DbContexttogether. I am not strong on Entity Framework. I thought that they are different things.

我看到了一段代码,混合DbSetDbContext在一起。我不擅长实体框架。我认为它们是不同的东西。

Can somebody give me a little explanation?

有人可以给我一点解释吗?

public class testContext : DbContext
{
    public testContext();

    public string IPAddress { get; set; }
    public DbSet<WSettings> Settings { get; set; }
    public string UserName { get; set; }

    public override int SaveChanges();
}

采纳答案by Miltos Kokkonidis

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!

直观地说,DbContext 对应于您的数据库(或数据库中的表和视图的集合),而 DbSet 对应于数据库中的表或视图。因此,您将获得两者的结合是完全有道理的!

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.

您将使用 DbContext 对象来访问您的表和视图(由 DbSet 表示),您将使用 DbSet 来访问、创建、更新、删除和修改您的表数据。

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:

如果您的数据库中有 10 个表并且您的应用程序使用其中的 5 个表(让我们称它们为 Table1 - Table 5),那么使用 MyAppContext 对象访问它是有意义的,其中 MyAppContext 类是这样定义的:

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:

请注意,例如,标识符 Table1 在定义的上下文类型中既用作类型的名称又用作属性的名称。你在上面看到的是非常典型的。下面给出了对应于表模式的类的示例:

public class Table1 
{
   public int Id {get; set;}
   public string AStringField {get; set;}
   //etc.
}

Have a look here for more information: http://entityframeworktutorial.net/

在这里查看更多信息:http: //entityframeworktutorial.net/

回答by Daniel A. White

DbContextgenerally represents a database connection and a set of tables. DbSetis used to represent a table.

DbContext一般代表一个数据库连接和一组表。DbSet用于表示表格。

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; }
}