possible to derive from DbSet RRS feed

  • Question

  • I Create class




    class Dict<TEntity> : DbSet<TEntity> where TEntity : class


    string Caption {get;set;} 


    and get error The type 'System.Data.Entity.DbSet<TEntity>' has no constructors defined 

    why I can't  to derive from DbSet?

    Thursday, July 22, 2010 10:27 AM

All replies

  • Hi, you're probably looking for IDbSet<TEntity> and implementing for you class.
    Jiri {x2} Cincura
    Thursday, July 22, 2010 1:10 PM
  • I try to create new class implementing IDbSet<TEntity> but in this case I have no table in database for this class.
    Friday, July 23, 2010 4:15 AM
  • Why? Table(s) are mapped to entites (that's the "TEntity") not sets.
    Jiri {x2} Cincura
    Friday, July 23, 2010 5:06 AM
  • Yes. I may create table using




    but how get TEntity sets in my  class implementing  IDbSet<TEntity>. This class is already null in context.

    Friday, July 23, 2010 6:05 AM
  • You can't "new" up a DbSet<T>, there is a factory method to get a new one that is part of DbContext.  There are no public constructors for DbSet<T>, this is true for the ObjectSet<T> object in the released version of EF 4 as well.  You a have to use a factory method from your context.

    When you create a class derived from DbContext like in the example walkthrus, there is a default initialization on any DbSet<T> properties with public setters.

    However, you can call your derived DbContext class and call the .Set<T> method.

    public class ProductCatalog : DbContext {
    	public IDbSet<Category> Categories { get; set; }
    	public IDbSet<Product> Products { get; set; }
    static void Main(string[] args) {
    	using (var context = new ProductCatalog()) {
              IDbSet<Product> products = context.Set<Products>();
    I've used it to create a Repository<T> class, obviously the context is a required dependency object for the repository.
    Friday, July 23, 2010 1:24 PM