我正在使用实体框架5的存储库模式。当我向用户实体添加新用户时,它不会保存到数据库中。任何想法为什么??
我有以下结构 -
DAL(包含实体框架模型) - >核心 - >网络
Form1.cs的
UserService _userService = new UserService();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = _userService.GetUserList(10, 1).Users;
listBox1.DisplayMember = "FullName";
}
private void btnAdd_Click(object sender, EventArgs e)
{
UserModel newUser = new UserModel();
newUser.Username = tbUsername.Text;
newUser.FirstName = tbFirstname.Text;
newUser.Surname = tbLastname.Text;
newUser.Password = tbPassword.Text;
newUser.LoginEnabled = true;
newUser.UserStatus = UserStatus.Active;
_userService.Add(newUser);
}
UserService.cs
public void Add(UserModel entity)
{
User newUser = new User();
newUser.DateCreated = DateTime.Now;
AutoMapper.Mapper.CreateMap<UserModel, User>();
try
{
_userRepository.Add(AutoMapper.Mapper.Map(entity, newUser));
}
catch (Exception ex)
{
}
}
RepositoryBase.cs
public abstract class RepositoryBase<T> : IRepository<T>
where T : class
{
public RepositoryBase()
: this(new AcRepositoryContext())
{
}
public RepositoryBase(IRepositoryContext repositoryContext)
{
repositoryContext = repositoryContext ?? new AcRepositoryContext();
_objectSet = repositoryContext.GetObjectSet<T>();
}
private IObjectSet<T> _objectSet;
public IObjectSet<T> ObjectSet
{
get
{
return _objectSet;
}
}
#region IRepository Members
public void Add(T entity)
{
this.ObjectSet.AddObject(entity);
}
public void Delete(T entity)
{
this.ObjectSet.DeleteObject(entity);
}
public IList<T> GetAll()
{
return this.ObjectSet.ToList<T>();
}
public IList<T> GetAll(Expression<Func<T, bool>> whereCondition)
{
return this.ObjectSet.Where(whereCondition).ToList<T>();
}
public T GetSingle(Expression<Func<T, bool>> whereCondition)
{
return this.ObjectSet.Where(whereCondition).FirstOrDefault<T>();
}
public void Attach(T entity)
{
this.ObjectSet.Attach(entity);
}
public IQueryable<T> GetQueryable()
{
return this.ObjectSet.AsQueryable<T>();
}
public long Count()
{
return this.ObjectSet.LongCount<T>();
}
public long Count(Expression<Func<T, bool>> whereCondition)
{
return this.ObjectSet.Where(whereCondition).LongCount<T>();
}
#endregion
}
AcRepositoryContext.cs
public class AcRepositoryContext : IRepositoryContext
{
private const string OBJECT_CONTEXT_KEY = "AC.DAL.AccessControlDBEntities";
public IObjectSet<T> GetObjectSet<T>()
where T : class
{
try
{
return ContextManager.GetObjectContext(OBJECT_CONTEXT_KEY).CreateObjectSet<T>();
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Returns the active object context
/// </summary>
public ObjectContext ObjectContext
{
get
{
return ContextManager.GetObjectContext(OBJECT_CONTEXT_KEY);
}
}
public int SaveChanges()
{
return this.ObjectContext.SaveChanges();
}
public void Terminate()
{
ContextManager.SetRepositoryContext(null, OBJECT_CONTEXT_KEY);
}
public DbSet<User> Users { get; set; }
public DbSet<Door> Doors { get; set; }
public DbSet<Event> Events { get; set; }
}
答案 0 :(得分:0)
在您的Repository实现类(RepositoryBase
或其子代)中,您应该调用上下文SaveChanges
。
例如,在RepositoryBase<T>
中,我会添加此方法:
public void Commit()
{
repositoryContext.SaveChanges()
}
在Add()
事件处理程序中对btnAdd_Click
进行调用后,应调用此Commit
方法将更改保存到数据库中。
有关更多信息,请查看此博客:
Using the Repository Pattern in Entity Framework
希望这有帮助!
答案 1 :(得分:0)
我在你的存储库中看不到你在AcRepositoryContext上调用SaveChanges我假设它继承自dbcontext或更可能来自你的命名约定objectcontext
因此,在添加新实体或更新或删除后,您需要保存更改以将这些更改保留到数据库中。
我可以在您的userservice类中看到您将实体添加到存储库,但之后您似乎没有在上下文中调用SaveChanges。