EntityFramework.sqlServerCompact and Repository pattern MVVM
本文关键字:pattern MVVM Repository and sqlServerCompact EntityFramework | 更新日期: 2023-09-27 18:05:28
我有一个WPF应用程序,我使用了MVVM架构,数据库sql server compact 4。Entityframework。首先是sqlserverCompact数据库,我使用了一个通用的存储库模式,如下所示:
public interface IRepository: IDisposable
{
IQueryable<T> All<T>() where T : class;
void InsertAsync<T>(T entity) where T: class ;
Task<T> GetByIdAsync<T>(int id) where T: class ;
Task EditAsync<T>(T entity) where T : class;
Task DeleteAsync<T>(T entity) where T : class;
}
实现Repository的类
public class TryitRepository : IRepository
{
private readonly MyEntityContext context;
public TryitRepository()
{
this.context = new MyEntityContext();
}
public IQueryable<T> All<T>() where T : class
{
return this.context.Set<T>();
}
public void Dispose()
{
if (this.context != null) this.context.Dispose();
}
public void InsertAsync<T>(T entity) where T : class
{
this.context.Set<T>().Add(entity);
this.context.SaveChanges();
}
}
//视图模型
public class ArticleViewModel : ObjectBase
{
public readonly IRepository article;
public ArticleViewModel()
{
article = new TryitRepository();
this.LoadData();
}
private void LoadCommand()
{
this.EditCommand = new CustomCommand(this.EditArticle, this.CanEditArticle);
this.SaveCommand= new CustomCommand(this.SaveArticle, this.CanSaveArticle);
this.DeleteCommand = new CustomCommand(this.DeleteArticle, this.CanDeleteArticle);
}
private bool CanSaveArticle(object obj)
{
return true;
}
private void SaveArticle(object obj)
{
Article artikel = new Article() { Name = "newarticle", Color = "Pink", Price = 125.95, Articlenr = 15547878, Items_In_Package= 12 };
article.InsertAsync<Article>(artikel);
this.LoadData();
}
当SaveArticle
被触发时,一切运行良好,我看到列表(ListView)上的新文章,但不在数据库中。我做错了什么?
解决!
见下面链接中ErikEJ的回答:
https://social.msdn.microsoft.com/forums/sqlserver/en us/e7f7a713 b0fd - 4716 a19d - 4421 e8be4c88/forcing - db -修改-在- sql - ce - -提交?forum=sqlce