简体   繁体   English

如何生成IDbSet而不是DbSet

[英]How to generate IDbSet instead of DbSet

I'm new to C# and EntityFramework and I need to maintain an active software. 我是C#EntityFramework新手,我需要维护一个活动的软件。 I needed to create a new table so I created it in the database and then updated my Model with EntityFramework. 我需要创建一个新表,以便在数据库中创建它,然后使用EntityFramework更新我的模型。

Howewer, it looks like the previous developer had been writing code directly in generated code (The Mode.Context.cs class) and EntityFramework when updating the model is wiping it and rewritting it completly. Howewer,看起来以前的开发人员在更新模型以擦拭并完全重写时,直接在生成的代码( Mode.Context.cs类)和EntityFramework直接编写了代码。

So I did a new partial class Model. 所以我做了一个新的局部类模型。 It looks like this : 看起来像这样:

public partial class Model : DbContext, IModel
    {
        public void SomeRandomMethod();
    }

And the generated Model looks like this : 生成的模型如下所示:

public partial class Model : DbContext
{
    public Model()
        : base("name=Model")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<RandomTable> ARandomTable { get; set; }
}

The problem, however, is that previously, the model was using IDbSet instead of DbSet and the interface IModel is asking for IDbSet<RandomTable> ARandomTable 但是,问题在于,以前,模型使用的是IDbSet而不是DbSet ,并且接口IModel要求IDbSet<RandomTable> ARandomTable

What would be the proper way to deal with that ? 处理该问题的正确方法是什么?

Normally the Dbcontext that represents your database has several DbSet<TEntity> properties, where every DbSet represents a table in your database. 通常,代表数据库的Dbcontext具有多个DbSet<TEntity>属性,其中每个DbSet代表数据库中的一个表。

All non-virtual properties of TEntity represent the columns in your table; TEntity所有非虚拟属性都代表表中的列; the virtual properties of TEntity represent the relations between tables: one-to-many, many-to-many etc. TEntity的虚拟属性表示表之间的关系:一对多,多对多等。

Every DbSet<TEntity> implements IDbSet<TEntity> so wherever your old DbContext used an IDbSet , you can give it the corresponding DbSet instead. 每个DbSet<TEntity>实现IDbSet<TEntity>因此无论您的旧DbContext使用了IDbSet ,您都可以给它相应的DbSet

If I understand you correctly, your old DbContext had some properties that implemented IDbSet<...> , and method SomeRandomMethod used these properties. 如果我理解正确,那么您的旧DbContext具有一些实现IDbSet<...>属性,并且方法SomeRandomMethod使用了这些属性。

class MyOldDbContext : DbContext
{
     public IDbSet<Aentity> Aentities {get; set;}
     public IDbSet<Bentity> BEntities {get; set;} 

     public void SomeRandomMethod()
     {   // this method uses the IdBsets AEntities and BEntities:
         IDbSet<AEntity> x = this.AEntities;
         IDbSet<BEntity> y = this.BEntities;
         ... // do something with x and y
     }
}

Now your new DbContext. 现在是您的新DbContext。 If the DbSets have the same Entity types as the old ones, there is no problem: 如果DbSet的实体类型与旧的相同,则没有问题:

class MyNewDbContext : DbContext
{
     public DbSet<Aentity> Aentities {get; set;}
     public DbSet<Bentity> BEntities {get; set;} 

     public void SomeRandomMethod()
     {   // this method uses the DbSets AEntities and BEntities, which implement IDbSet
         IDbSet<AEntity> x = this.AEntities;
         IDbSet<BEntity> y = this.BEntities;
         ... // do something with x and y
     }
}

Note that AEntities/BEntities are now DbSet<...> instead of IDbSet<...> . 请注意,AEntities / BEntities现在是DbSet<...>而不是IDbSet<...> Because every DbSet<...> implements IDbSet<...> , your problem is solved. 因为每个DbSet<...>实现IDbSet<...> ,所以可以解决您的问题。

It is a bit more difficult if your new tables are different than your old ones. 如果新表与旧表不同,则要困难一些。 In that case you'll have to write adapter properties, that return the expected IDbSet<...> 在这种情况下,您必须编写适配器属性,该属性将返回预期的IDbSet<...>

class MyNewDbContext : DbContext
{
     public DbSet<Teacher> Teachers {get; set;}
     public DbSet<Student> Students {get; set;} 

     public void SomeRandomMethod()
     {   // this method uses the IdBset of AEntities and BEntities:
         IDbSet<AEntity> x = this.AEntities;
         IDbSet<BEntity> y = this.BEntities;
         ... // do something with x and y
     }

     // for SomeRandomMethod we need properties AEntities and BEntities
     // use your new DbSets to mimic the old AEntities and BEntities
     private IDbSet<AEntity> AEntities => this.Teachers
          .Join(this.Students, ...)
          .Where(...)
          .Select(...);
      // similar for BEntities
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM