我有DataContext类(继承了DbContext)
public class DataContext : DbContext
{
public DataContext() : base("Default")
{
}
public DbSet<Scale> Scales { get; set; }
public DbSet<Administrator> Administrators { get; set; }
public DbSet<User> Users { get; set; }
}
我还有一个BusinessContext类,在其中我对数据库执行所有操作,如添加新的,编辑和删除实体。
public sealed class BusinessContext : IDisposable
{
private readonly DataContext context;
private bool disposed;
public BusinessContext()
{
context = new DataContext();
}
public DataContext DataContext
{
get
{
return context;
}
}
#region ScaleScenarios
public void AddNewScale(Scale scale)
{
context.Scales.Add(scale);
context.SaveChanges();
}
public void UpdateScale(Scale scale)
{
context.Entry(scale).CurrentValues.SetValues(scale);
context.SaveChanges();
}
public ICollection<Scale>GetAllScales()
{
return context.Scales.OrderBy(prop => prop.Id).ToArray();
}
#endregion
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposed || !disposing)
{
return;
}
if (context != null)
{
context.Dispose();
}
disposed = true;
}
#endregion
}
现在应该为其他实体(用户,管理员)创建一个单独的BusinessContext还是应该将所有实体的所有操作归为一类?另外,当我打开一个新对话框(用于登录)时,应该像在MainWindow中那样将父ViewModel的BusinessContext传递给DialogViewModel还是在DialogViewModel的构造函数中创建一个新的BusinessContext实例?