具有DBSet <some iinterface =“”>的dbContext

时间:2018-10-09 15:41:45

标签: c# entity-framework interface repository dbcontext

我有以下高度简化的代码可以尝试在 DBset(请参见下文)。

当我从上下文中删除具有接口的DBSet并运行时,

  var r =  locaDatabase.Entities.First(); ---- WORKS AS EXPECTED

但是,当我按如下所示将接口的DBSet添加到上下文中时。它 不会编译。它给出了一个错误,指出从DbSet进行了强制转换 不能(DbSet)EntityAddresses。

CS0030:无法将类型从...转换为...。

我已经在堆栈溢出中彻底搜索了答案,并尝试了很多事情,但都没有奏效。 .Cast,Set等更多...

似乎甚至存在一个更根本的问题。

大图是这个。我想为我的上下文创建一个接口,创建接口 对于我的实体,然后使包含该应用程序的程序集(引用EF Core) 使用上下文接口和实体作为接口。

我知道这确实是简化的代码,但是该应用程序确实可以工作。直到我开始转换 它改为使用接口。注意:我也有用于上下文的界面,但尚未开始 关于使用该接口进行测试,我想从简单尝试首先使用实体​​接口开始, 然后继续研究更深入的内容。

谢谢

    public class LocalSQLiteDatabaseDbContext : DbContext
    {

     public virtual DbSet<EntityAddress> EntityAddresses
     {
        get;

        set;
     }

     public virtual DbSet<IEntityAddress> EntityAddresses
     {
        get
          {
           return (DbSet<IEntityAddress>)EntityAddresses;
          }


        set 
          {
           .....
          }

       ...... the rest.....

     }// End class

    public class EntityAddress : IEntityAddress, INotifyPropertyChanging, INotifyPropertyChanged 
    {

         private long _EntityAddressID;

         private string _Street;

        ...... the rest.....

    } // End class


    public interface IEntityAddress
    {

      long EntityAddressID { get; set; }

      string Street { get; set; }

      ...... the rest.....

    } // End interface

0 个答案:

没有答案