简体   繁体   English

IDbSet 和 DbSet 有什么区别?

[英]What is the difference between IDbSet and DbSet?

What is the difference between有什么区别

public IDbSet<Chrip> Chirps { get; set; } 

and

public DbSet<Chrip> Chirps { get; set; }  

Are they the same?它们相同吗?

Sam I am's answer neatly defines the difference between an interface and a class, but there are additional considerations when choosing which to use in your code. Sam我的答案巧妙地定义了接口和类之间的区别,但在选择要在代码中使用的内容时还有其他注意事项。

Specifically - a class can implement more than one interface, or can implement methods and properties not defined by any interface. 具体而言 - 一个类可以实现多个接口,或者可以实现任何接口未定义的方法和属性。

In this case, the IDbSet<TEntity> interface defines most of the methods and properties used by the DbSet<TEntity> class, but not all of them. 在这种情况下, IDbSet<TEntity>接口定义了DbSet<TEntity>类使用的大多数方法和属性,但不是全部。 For example, the FindAsync , RemoveRange and SqlQuery methods only exist on the concrete class implementation. 例如, FindAsyncRemoveRangeSqlQuery方法仅存在于具体的类实现上。 If you use the interface in your code, you won't have those particular methods available without first casting to the concrete type. 如果在代码中使用该接口,则在没有首先转换为具体类型的情况下,您将无法使用这些特定方法。

Also, the Remarks section in the MSDN for IDbSet<TEntity> has another interesting point: 此外, MSDN中的IDbSet<TEntity>的备注部分有另一个有趣的观点:

IDbSet<TEntity> was originally intended to allow creation of test doubles (mocks or fakes) for DbSet<TEntity> . IDbSet<TEntity>最初旨在允许为DbSet<TEntity>创建测试双精度( DbSet<TEntity>或伪造)。 However, this approach has issues in that adding new members to an interface breaks existing code that already implements the interface without the new members. 但是,这种方法存在的问题是,在接口中添加新成员会破坏已经实现接口而没有新成员的现有代码。 Therefore, starting with EF6, no new members will be added to this interface and it is recommended that DbSet<TEntity> be used as the base class for test doubles. 因此,从EF6开始,不会向此接口添加新成员,建议将DbSet<TEntity>用作测试双精度的基类。

I believe it it safe to extend that line of thinking to say that you should usually declare your properties using DbSet<TEntity> instead of IDbSet<TEntity> unless you have a good reason not to. 我相信扩展这种思路可以说你通常应该使用DbSet<TEntity>而不是IDbSet<TEntity>声明你的属性是IDbSet<TEntity>除非你有充分的理由不这样做。

Usually, when a type name begins with an I it is an interface. 通常,当类型名称以I开头时,它是一个接口。 This is not a hard rule, but just a naming convention. 这不是一个硬规则,而只是一个命名约定。

DbSet probably implements IDbSet DbSet可能实现了IDbSet


Suppose you have an interface like this 假设你有这样的界面

public interface IFoo
{
    int Echo(int bar);
}

A class would implement an interface like this 一个类会实现这样的接口

public class Foo : IFoo
{
    public int Echo(int bar)
    {
        return bar;
    }
}

What this means is that your class Foo must implement every method the interface says it does, which in this case is a method named Echo which takes an int as input, and returns an int . 这意味着你的类Foo必须实现接口所说的每个方法,在这种情况下是一个名为Echo的方法,它接受一个int作为输入,并返回一个int

What this allows you to do is to is to treat a bunch of different classes the same way, even if you don't know how they're implemented. 这允许你做的是以相同的方式处理一堆不同的类,即使你不知道它们是如何实现的。 For more information, see Polymorphism . 有关更多信息,请参阅多态性

They refer to different namespaces.它们引用不同的命名空间。 One is for EF Core and the other is not recommended.一种适用于 EF Core,另一种不推荐使用。

Microsoft.EntityFrameworkCore.DbSet<T>
System.Data.Entity.IDbSet<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IDbSet.Add和DbEntityEntry.State = EntityState.Added有什么区别? - What is the difference between IDbSet.Add and DbEntityEntry.State = EntityState.Added? 如何生成IDbSet而不是DbSet - How to generate IDbSet instead of DbSet 包装DbSet <TEntity> 使用自定义DbSet / IDbSet? - Wrapping DbSet<TEntity> with a custom DbSet/IDbSet? 在EF7中使用dbset和mappers有什么区别 - what is the difference between use dbset and mappers in EF7 DbSet之间的区别 <T> 财产和集 <T> EF Core中的()函数? - Difference between DbSet<T> property and Set<T>() function in EF Core? 手动添加DbSet或允许自动创建表之间有什么区别? - Is there any difference between manually adding a DbSet or allowing the table to be automatically created? 设置什么 <TEntity> 在IDbSet接口中做什么? - What does Set<TEntity> do in IDbSet interface? 实体框架6 DbSet AddRange vs IDbSet Add - AddRange如何更快? - Entity Framework 6 DbSet AddRange vs IDbSet Add - How Can AddRange be so much faster? 如何在 IdentityUser DbSet 和其他 DbSet 之间加入 - How to join between IdentityUser DbSet and other DbSet DbSet 和 ObservableCollection 之间的转换 - Conversion between DbSet and ObservableCollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM