Asp.net mvc 2 使用EF4MVC2、存储库和工作单元模式,发布数据更新

Asp.net mvc 2 使用EF4MVC2、存储库和工作单元模式,发布数据更新,asp.net-mvc-2,entity-framework-4,repository,Asp.net Mvc 2,Entity Framework 4,Repository,我将MVC2与EntityFramework4结合使用,并尝试实现一个存储库和UnitofWork模式。我的添加和删除工作正常,但是当编辑被称为_context.SaveChanges()时,会将新更改保存到数据库中。我已在调试中逐步完成了代码,并看到新值在传递给控制器中编辑函数的对象中,但在调用提交时,不会更新任何内容。请参阅下面的代码:感谢您的帮助 这是我的假设 namespace EventScheduling.DataModel.Custom { public interface

我将MVC2与EntityFramework4结合使用,并尝试实现一个存储库和UnitofWork模式。我的添加和删除工作正常,但是当编辑被称为_context.SaveChanges()时,会将新更改保存到数据库中。我已在调试中逐步完成了代码,并看到新值在传递给控制器中编辑函数的对象中,但在调用提交时,不会更新任何内容。请参阅下面的代码:感谢您的帮助

这是我的假设

namespace EventScheduling.DataModel.Custom
{
    public interface IRepository<T>

    {
        void Add(T newEntity);
        void Remove(T entity);       
        IQueryable<T> Find(Expression<Func<T, bool>> predicate);
        IQueryable<T> FindAll();        
    }
}

当您想更新EF中的实体时,必须首先将实体附加到
ObjectContext
实例或相关的
ObjectSet
实例,并使用
ObjectStateManager.ChangeObjectState
将实体状态设置为
EntityState.Modified

另一种可能性是首先从数据库加载实体,并将更改直接合并到此实体中


选中注释中的链接以获取示例或尝试使用搜索框。这个问题是EF标签中最常见的问题之一。

可能重复[how to update a entity Framework 4.NET中的实体]()EF的上下文已经是工作单元的实现,顺便说一句。为什么我需要将实体附加到objectcontext,英孚不应该跟踪这些变化吗?@Kmf:英孚应该在哪里跟踪这些变化?您正在使用ASP.NET MVC。当您向客户机发送响应时,该实体将丢失,当编辑请求到达时,默认的模型绑定器将创建一个新实体。即使您在会话中使用自定义模型绑定器和存储实体,您的ObjectContext可能已经关闭,因此无法跟踪任何更改。谢谢,这很有效。我想我只是对它何时以及为什么丢失对应该附加到的上下文的引用感到困惑。@Kmf:这取决于UnitOfWork/ObjectContext的生存期。你不应该分享它们。为每个请求使用新实例。Web应用程序是分离的场景—您使用的实体与上下文失去连接,或者创建为断开连接并连接到上下文。
namespace EventScheduling.DataModel.Custom
{
    public class SQLRepository<T>:IRepository<T> where T : class

    {
        protected ObjectSet<T> _objectSet;

        public SQLRepository(ObjectContext context)
        {
            _objectSet = context.CreateObjectSet<T>();
        }

        public IQueryable<T> Find(Expression<Func<T, bool>> predicate){

        return _objectSet.Where(predicate);
        }
        public void Add(T newEntity)
        {
         _objectSet.AddObject(newEntity);
        }
        public void Remove(T entity)
        {
        _objectSet.DeleteObject(entity);
        }

        public IQueryable<T> FindAll()
        {
        return _objectSet;
        }    
    }
}
namespace EventScheduling.DataModel.Custom
{
    public interface IUnitOfWork
    {
        IRepository<utility_event> utility_event { get; }
        IRepository<event_activity> event_activity { get; }
        IRepository<employee>  employee{ get; }
        IRepository<activity_resource> activity_resource  { get; }
        IRepository<Elmah_Error>  Elmah_Error { get; }
        IRepository< location> location { get; }
        IRepository<location_station> location_station  { get; }
        IRepository<registration_type> registration_type  { get; }
        IRepository< resource> resource  { get; }
        IRepository<shift> shift { get; }
        IRepository<shift_person> shift_person{ get; }
        IRepository<event_type> event_type { get; }
        IRepository<status> status { get; }

        void Commit();
    }
}
namespace EventScheduling.DataModel.Custom
{
    public class SqlUnitOfWork: IUnitOfWork
    {
        readonly ObjectContext _context;
        const String ConnectionStringName = "EventEntities";


        public SqlUnitOfWork()
        {            
            var connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString;
            _context = new ObjectContext(connectionString);
            _context.ContextOptions.LazyLoadingEnabled = true;

        }

        public IRepository<utility_event> utility_event
        {
            get {
                if (_utilityEvent == null)
                {
                    _utilityEvent = new SQLRepository<utility_event>(_context);
                }
                return _utilityEvent;
            }
        }

        public IRepository<event_activity> event_activity
        {
            get
            {
                if (_eventActivities == null)
                {
                    _eventActivities = new SQLRepository<event_activity>(_context);
                }
                return _eventActivities;
            }
        }

        public IRepository<employee> employee
        {
            get
            {
                if (_employees == null)
                {
                    _employees = new SQLRepository<employee>(_context);
                }
                return _employees;
            }
        }

        public IRepository<activity_resource> activity_resource
        {
            get
            {
                if (_activityResources == null)
                {
                    _activityResources = new SQLRepository<activity_resource>(_context);
                }
                return _activityResources;
            }
        }

        public IRepository<location> location
        {
            get
            {
                if (_locations == null)
                {
                    _locations = new SQLRepository<location>(_context);
                }
                return _locations;
            }
        }

        public IRepository<location_station> location_station
        {
            get
            {
                if (_locationStations == null)
                {
                    _locationStations = new SQLRepository<location_station>(_context);
                }
                return _locationStations;
            }
        }

        public IRepository<registration_type> registration_type
        {
            get
            {
                if (_registrationTypes == null)
                {
                    _registrationTypes = new SQLRepository<registration_type>(_context);
                }
                return _registrationTypes;
            }
        }
        public IRepository<resource> resource
        {
            get
            {
                if (_resources == null)
                {
                    _resources = new SQLRepository<resource>(_context);
                }
                return _resources;
            }
        }


        public IRepository<shift> shift
        {
            get
            {
                if (_shifts == null)
                {
                    _shifts = new SQLRepository<shift>(_context);
                }
                return _shifts;
            }
        }


        public IRepository<shift_person> shift_person
        {
            get
            {
                if (_shiftPersons == null)
                {
                    _shiftPersons = new SQLRepository<shift_person>(_context);
                }
                return _shiftPersons;
            }
        }

        public IRepository<Elmah_Error> Elmah_Error
        {
            get
            {
                if (_ElmahError == null)
                {
                    _ElmahError = new SQLRepository<Elmah_Error>(_context);
                }
                return _ElmahError;
            }
        }

        public IRepository<event_type> event_type
        {
            get
            {
                if (_eventTypes == null)
                {
                    _eventTypes = new SQLRepository<event_type>(_context);
                }
                return _eventTypes;
            }
        }

        public IRepository<status> status
        {
            get
            {
                if (_status == null)
                {
                    _status = new SQLRepository<status>(_context);
                }
                return _status;
            }
        }

        public void Commit()
        {  
            _context.SaveChanges();
        }

        SQLRepository<utility_event> _utilityEvent = null;
        SQLRepository<event_activity> _eventActivities = null;
        SQLRepository<employee> _employees = null;
        SQLRepository<activity_resource> _activityResources = null;
        SQLRepository<Elmah_Error> _ElmahError = null;
        SQLRepository<location> _locations = null;
        SQLRepository<location_station> _locationStations = null;
        SQLRepository<registration_type> _registrationTypes = null;
        SQLRepository<resource> _resources  = null;
        SQLRepository<shift> _shifts  = null;
        SQLRepository<shift_person> _shiftPersons  = null;
        SQLRepository<event_type> _eventTypes = null;
        SQLRepository<status> _status = null;
    }
}
public ActionResult Edit(int id, shift e)
{
    if (!ModelState.IsValid)
    {
        return View(e);
        //return to view
    }
    else
    {
        e.shift_begin = (DateTime)e.shift_date.Value.Add(e.shift_begin.Value.TimeOfDay);
        e.shift_end = (DateTime)e.shift_date.Value.Add(e.shift_end.Value.TimeOfDay);

        _unitOfWork.Commit();
        return RedirectToAction("Details", "EventActivity", new { id = e.activity_id });
    }

}