实体框架代码第一个对象引用未设置但存在于数据库中

时间:2013-10-24 10:13:00

标签: c# entity-framework sql-server-ce

我正在努力解决直到现在才发生的问题。 我尝试使用带有SQL CE数据库的EF Code First实现一个应用程序。 我有这两个主要课程给我提出具体问题:

public class Session
{
    public int Id { get; set; }

    public Guid Unique { get; set; }
    public DateTime DateTime { get; set; }

    public virtual Patient Patient { get; set; }
    public virtual Test Test { get; set; }

    public virtual List<VideoExerciseResult> VideoSessionResults { get; set; }
}

public class VideoExerciseResult
{
    public int Id { get; set; }
    public Guid Unique { get; set; }        
    public string Word { get; set; }
    public bool IsAnswerCorrect { get; set; }

    public virtual Session Session { get; set; }
}

当我完成一个会话并保存结果时,我可以完成该操作,并将会话和结果保存在数据库中。 我可以检查直接在.sdf文件中查找的字段,并且存在对VideoExerciseResults列中的Session_Id的引用。

但是在代码中我尝试进行此操作:

private void GetSessionData()
    {
        List<VideoExerciseResult> tempList2 = new List<VideoExerciseResult>(UOW.VideoSessionsResults.GetAll());
        ListOfVideoSessionResults = new ObservableCollection<VideoExerciseResult>((tempList2.Where(ver => ver.Session.Id == SelectedSession.Id)).ToList());
    }

它给出了错误消息:

  

未将对象引用设置为对象的实例。

事实上,如果我将鼠标悬停在tempList2上,某些videoexerciseresult对象会将Session设置为null,甚至认为.sdf文件中有一个值..

我错过了什么吗?


我将添加有关Session的代码... 它基于我遵循的存储库和工作单元模式

public class EnPleinProjectUOW : IEnPleinProjectUOW, IDisposable
{
    private EnPleinProjectDbContext DbContext { get; set; }

    public IRepositoryProvider RepositoryProvider { get; set; }

    public IVideoExerciseResultRepository VideoSessionsResults
    {
        get { return GetRepo<IVideoExerciseResultRepository>(); }
    }

    public IPatientRepository Patients
    {
        get { return GetRepo<IPatientRepository>(); }
    }

    public ISessionRepository Sessions
    {
        get { return GetRepo<ISessionRepository>(); }
    }

    public ITestRepository Tests
    {
        get { return GetRepo<ITestRepository>(); }
    }

    public IImageFileRepository ImageFiles
    {
        get { return GetRepo<IImageFileRepository>(); }
    }

    // We need inverse of control
    public EnPleinProjectUOW(IRepositoryProvider repositoryProvider)
    {
        CreateDbcontext();

        repositoryProvider.DbContext = DbContext;
        RepositoryProvider = repositoryProvider;
    }

    private void CreateDbcontext()
    {
        DbContext = new EnPleinProjectDbContext();

        DbContext.Configuration.ProxyCreationEnabled = false;

        DbContext.Configuration.LazyLoadingEnabled = false;

        DbContext.Configuration.ValidateOnSaveEnabled = false;
    }

    private IRepository<T> GetStandardRepo<T>() where T : class
    {
        return RepositoryProvider.GetRepositoryForEntityType<T>();
    }
    private T GetRepo<T>() where T : class
    {
        return RepositoryProvider.GetRepository<T>();
    }

    public void Commit()
    {
        DbContext.SaveChanges();
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (DbContext != null)
            {
                DbContext.Dispose();
            }
        }
    }       
}

这是针对一般命令的:

public class EnPleinProjectRepository<T> : IRepository<T> where T : class
{
    #region Properties

    protected DbContext DbContext { get; set; }
    protected DbSet<T> DbSet { get; set; }

    #endregion

    #region Constructor

    public EnPleinProjectRepository(DbContext dbContext)
    {
        if (dbContext == null)
            throw new ArgumentNullException("Dbcontext missing");

        DbContext = dbContext;
        DbSet = DbContext.Set<T>();

    }

    #endregion

    #region Methods

    public IQueryable<T> GetAll()
    {
        return DbSet;
    }

    public T GetById(int id)
    {
        return DbSet.Find(id);
    }

    public void Add(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != System.Data.EntityState.Detached)
            dbEntityEntry.State = System.Data.EntityState.Added;
        else
        {
            DbSet.Add(entity);
        }
    }

    public void Update(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State == System.Data.EntityState.Detached)
        {
            DbSet.Attach(entity);
        }

        dbEntityEntry.State = System.Data.EntityState.Modified;
    }

    public void Delete(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != System.Data.EntityState.Deleted)
        {
            dbEntityEntry.State = System.Data.EntityState.Deleted;
        }
        else
        {
            DbSet.Attach(entity);
            DbSet.Remove(entity);
        }
    }

    public void Delete(int id)
    {
        var entity = GetById(id);
        if (entity == null) return;
        Delete(entity);
    }

    public void DeleteAll()
    {
        foreach (T entity in DbSet)
            Delete(entity);
    }

但我',非常有信心这是有效的,因为在数据库中保存了数据和所有参考资料......我不知道为什么其中一些没有被回读......

感谢您的回答

1 个答案:

答案 0 :(得分:0)

我发现在单行中执行表达式有效:

ListOfVideoSessionResults = new ObservableCollection<VideoExerciseResult>(UOW.VideoSessionsResults.GetAll().Where(vsr => vsr.Session.Id == SelectedSession.Id));