【问题标题】:New entity not comiting to database新实体未提交到数据库
【发布时间】:2014-06-29 14:13:33
【问题描述】:

我在实体框架 5 中使用存储库模式。当我向用户实体添加新用户时,它不会保存到数据库中。任何想法为什么??

我有以下结构 -

DAL(包含实体框架模型)-> 核心-> 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);
    }

用户服务.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; }
}

【问题讨论】:

    标签: c# entity-framework entity-framework-5 repository-pattern


    【解决方案1】:

    在您的存储库实现类(RepositoryBase 或其子级)中,您应该调用上下文的SaveChanges

    例如,在RepositoryBase&lt;T&gt;,我会添加这个方法:

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

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

    有关更多信息,请查看此博客:

    Using the Repository Pattern in Entity Framework

    希望这会有所帮助!

    【讨论】:

    • 我已将 AcRepositoryContext.cs 文件添加到帖子中。我似乎无法从 RepositoryBase? 访问任何 SaveChanges() 方法
    • 嗯,保存需要通过您的 Repository Context 类进行,据我所知,该类仅在您的 RepositoryBase 类中可用。因此我的建议。但是,您更了解您的对象层次结构和继承,因此请使用您更好的判断将其放置在适当的位置。 :)
    【解决方案2】:

    我在您的存储库中看不到您在 AcRepositoryContext 上调用 SaveChanges 的位置,我假设这继承自 dbcontext 或更可能来自您的命名约定 objectcontext

    因此,在您添加新实体或更新或删除实体后,您需要保存更改以将这些更改持久保存到数据库中。

    我可以在您的用户服务类中看到您将实体添加到存储库,但之后您似乎没有在上下文中调用 SaveChanges。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-18
      • 2012-12-15
      • 2013-05-26
      • 2011-03-21
      • 2013-06-03
      • 2022-11-13
      • 2018-06-08
      • 1970-01-01
      相关资源
      最近更新 更多