新实体未提交到数据库

本文关键字:提交 数据库 实体 新实体 | 更新日期: 2023-09-27 18:10:46

我使用实体框架5的存储库模式。当我向我的users实体添加一个新用户时,它不会被保存到数据库中。知道为什么吗?

我有以下结构-

DAL(包含实体框架模型)-> Core -> Web

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; }
}

新实体未提交到数据库

在您的Repository实现类(RepositoryBase或它的子类)中,您应该调用上下文的SaveChanges

例如,在RepositoryBase<T>中,我会添加这个方法:

public void Commit()
{
   repositoryContext.SaveChanges()
}

在您的btnAdd_Click事件处理程序中执行对Add()的调用之后,您应该调用这个Commit方法来保存对数据库的更改。

更多信息,请看这个博客:

在实体框架中使用存储库模式

希望这对你有帮助!

我看不到在你的存储库中你调用SaveChanges在你的AcRepositoryContext我假设这继承自dbcontext或更可能从你的命名约定objectcontext

所以当你添加了一个新的实体或更新或删除之后,你需要保存这些更改以使这些更改持久化到数据库中。

我可以看到在你的userservice类中,你添加一个实体到存储库,但你似乎没有在上下文之后调用SaveChanges