【问题标题】:C# wpf DbContext Cannot implicitly convert typeC# wpf DbContext 不能隐式转换类型
【发布时间】:2018-01-26 13:02:23
【问题描述】:

我的代码有问题。在TegevusedProjektis = _projektService.GetAllTegevusedProjektides;MainWindowVM.Cs 类中,它显示以下错误:

我不知道如何解决它,谁能帮助我。

我的MainWindowVM.cs 代码:

public class MainWindowVM : BaseVM
{
    private List<Projekt> _projekts;
    private List<Tegevus> _tegevused;
    private List<TegevusProjektis> _tegevusedProjektis;

    private IProjektInterface _projektService;

    public MainWindowVM()
    {
        _projekts = new List<Projekt>();
        _tegevused = new List<Tegevus>();
        _tegevusedProjektis = new List<TegevusProjektis>();
        _projektService = new ProjektService(new ProjektDbContext());
    }

    public void LoadData()
    {
        Projektid = _projektService.GetAllProjekts();
        Tegevused = _projektService.GetAllTegevused();
        TegevusedProjektis = _projektService.GetAllTegevusedProjektides;
    }

    public List<Projekt> Projektid
    {
        get { return _projekts; }
        set
        {
            _projekts = value;
            base.NotifyPropertyChanged("Projekts");
        }
    }

    public List<Tegevus> Tegevused
    {
        get { return _tegevused; }
        set
        {
            _tegevused = value;
            base.NotifyPropertyChanged("Tegevused");
        }
    }

    public List<TegevusProjektis> TegevusedProjektis
    {
        get { return _tegevusedProjektis; }
        set
        {
            _tegevusedProjektis = value;
            base.NotifyPropertyChanged("TegevusedProjektis");
        }
    }


}

这是我的ProjectService.cs 代码:

 public class ProjektService : BaseService
{
    public ProjektService(ProjektDbContext ctx) : base(ctx)
    {
    }

    public List<Projekt> GetAllProjekts()
    {
        return base.DataContext.Projektid.ToList();
    }

    public List<Tegevus> GetAllTegevused()
    {
        return base.DataContext.Tegevused.ToList();
    }

    public List<TegevusProjektis> GetAllTegevusedProjektis()
    {
        return base.DataContext.TegevusedProjektides.ToList();
    }

    public Projekt GetProjektById(int id)
    {
        return DataContext.Projektid.Where(x => x.ProjektId == id).Single();
    }

    public Tegevus GetTegevusById(int id)
    {
        return DataContext.Tegevused.Where(x => x.TegevusId == id).Single();
    }

    public TegevusProjektis GetTegevusProjektisById(int id)
    {
        return DataContext.TegevusedProjektides.Where(x => x.TegevusProjektisId == id).Single();
    }

【问题讨论】:

  • 向我们展示ProjektService 的类定义。我很确定它没有实现您期望的IProjektInterface
  • 在编译器提到的显式转换时是否出错?
  • 添加ProjektService代码
  • ProjektService.cs 似乎不包含函数 GetAllTegevusedProjektides?是在 BaseService 中吗?

标签: c# wpf visual-studio entity-framework


【解决方案1】:

我在下面捕获了您的代码的相关部分。

public class MainWindowVM : BaseVM
{
    private IProjektInterface _projektService;

    public MainWindowVM()
    {
        _projektService = new ProjektService(new ProjektDbContext());
    }
}

public class ProjektService : BaseService
{ }

照原样,ProjektService 没有实现 IProjektInterface。您可以通过以下两种方式之一解决此问题。

您可以更改私有字段的类型。

private ProjektService _projektService;

或者你可以在ProjektService中实现你的接口。

public class ProjektService : BaseService, IProjektInterface
{ }

【讨论】:

    猜你喜欢
    • 2021-06-08
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    相关资源
    最近更新 更多