使用实体框架进行部分更新 - ASP.NET MVC3 C#

Sli*_*nky 1 c# entity-framework asp.net-mvc-3

我试图只更新表中的一些字段.我创建了一个包含需要更新的字段的视图模型.表格中还有其他字段不需要触摸,因此它们已经留在视图模型中.

当我执行SaveChanges()时,我得到一个关于视图模型中未包含的字段的错误,不能为NULL.由于这是部分更新,我认为视图模型中未包含的字段应该只保留在更新上.

例外情况:

Cannot insert the value NULL into column 'NewClubName', table 'dbo.NewClub'; 
column does not allow nulls. UPDATE fails
Run Code Online (Sandbox Code Playgroud)

这是我有的:

//View Model
    public class FormANewClubTeamViewModel
    {
        /******************************************************
            Properties for Domain Model "NewClub"
        *******************************************************/
        public int NewClub_Id { get; set; }

        //District and Division
        public string District { get; set; }
        public string Division { get; set; }

        //Lt Governor
        public string LtGovMasterCustomerId { get; set; }
        public string LtGovContact { get; set; }
        public string LtGovEmail { get; set; }
        public string LtGovPhone { get; set; }

        // Club Counselor
        public string ClubCounselorMasterCustomerId { get; set; }

        [Display(Name = "Club counselor")]
        [Required(ErrorMessage = "Club counselor name")]
        public string ClubCounselorContact { get; set; }

        [Display(Name = "Club counselor email")]
        [Required(ErrorMessage = "Club counselor email")]
        public string ClubCounselorEmail { get; set; }

        [Display(Name = "Club counselor phone")]
        [Required(ErrorMessage = "Club counselor phone")]
        public string ClubCounselorPhone { get; set; }

        /******************************************************
           Properties for Domain Model "NewClubSponsor"
       *******************************************************/
        public List<NewClubSponsor> Sponsors { get; set; }
    }


 //Controller doing the update
        if (ModelState.IsValid)
        {

            if (model.NewClub_Id > 0)
            {
                httpStatus = HttpStatusCode.OK;

                NewClub newClub = new NewClub
                    {
                        Id = model.NewClub_Id,
                        ClubCounselorMasterCustomerId = model.ClubCounselorMasterCustomerId,
                        ClubCounselorContact = model.ClubCounselorContact,
                        ClubCounselorEmail = model.ClubCounselorEmail,
                        ClubCounselorPhone = model.ClubCounselorPhone,
                        DateUpdated = DateTime.Now


                    };

                db.NewClubs.Add(newClub);
                db.Entry(newClub).State = EntityState.Modified;

                try
                {
                    var dbResult = db.SaveChanges() > 0;

                }
                catch (SqlException ex)
                { 
                   [...]
                }
Run Code Online (Sandbox Code Playgroud)

And*_*rei 5

这里的问题是EF没有任何意义知道您是将这个特定属性更新为NULL,还是根本不想更新它.

据我所知,没有办法在EF中进行这样的部分更新(除非您愿意将一些纯SQL代码引入数据访问层),就像现在一样.这意味着要进行更新,您需要加载实体,更新其字段并保存更改:

if (model.NewClub_Id > 0)
{
    httpStatus = HttpStatusCode.OK;

    NewClub newClub = db.NewClubs.Single(nc => nc.Id == model.NewClub_Id);

    newClub.ClubCounselorMasterCustomerId = model.ClubCounselorMasterCustomerId;
    newClub.ClubCounselorContact = model.ClubCounselorContact;
    newClub.ClubCounselorEmail = model.ClubCounselorEmail;
    newClub.ClubCounselorPhone = model.ClubCounselorPhone;
    newClub.DateUpdated = DateTime.Now;

    try
    {
        var dbResult = db.SaveChanges() > 0;
    }
    catch (SqlException ex)
    { 
       [...]
    }
}
Run Code Online (Sandbox Code Playgroud)