我一直在努力解决一个直到现在我才想到的问题。我尝试使用带有 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; }
}
当我完成会话并保存结果时,EF 我可以完成操作并将会话和结果保存在数据库中。我可以检查直接在 .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 文件中有一个值也是如此。
我错过了什么?
我将添加有关会话的代码...它基于我遵循的存储库和工作单元模式
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);
}
但是我非常有信心,因为在数据库中保存了数据并且所有引用都被保存了......我不知道为什么其中一些没有被读回......
谢谢你的回答