gpt4 book ai didi

c# - 使用 Entity Framework 进行部分更新 - ASP.NET MVC3 C#

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:45 26 4
gpt4 key购买 nike

我试图只更新表中的某些字段。我创建了一个包含需要更新的字段的 View 模型。表中还有其他不需要触及的字段,因此它们已离开 View 模型。

当我执行 SaveChanges() 时,我收到有关 View 模型中未包含的字段不能为 NULL 的错误。由于这是部分更新,我认为未包含在 View 模型中的字段应该在更新中单独保留。

异常:

Cannot insert the value NULL into column 'NewClubName', table 'dbo.NewClub'; 
column does not allow nulls. UPDATE fails

这是我的:

//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)
{
[...]
}

最佳答案

这里的问题是 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)
{
[...]
}
}

关于c# - 使用 Entity Framework 进行部分更新 - ASP.NET MVC3 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20978287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com