gpt4 book ai didi

c# - dbContext.SaveChanges() 不保存也不输出错误

转载 作者:行者123 更新时间:2023-11-30 17:46:23 24 4
gpt4 key购买 nike

我正在尝试更新我的 MVC 项目中的用户对象,但它似乎没有保存我输入的新值。

谁能发现我做错了什么?

这是我进行保存的方法,我知道我通过运行 Debug.WriteLines() 一直到 db.SaveChanges(); .

public bool editUser(string email, User innUser)
{
var db = new PastaContext();
try
{
User editUser = db.Users.Find(email);
editUser.Firstname = innUser.Firstname;
Debug.WriteLine(innUser.Firstname);
Debug.WriteLine(editUser.Firstname);
editUser.Surname = innUser.Surname;
editUser.Address = innUser.Address;
editUser.Phonenr = innUser.Phonenr;
var newUser = new dbUser();
byte[] passwordDb = createHash(innUser.Password);
newUser.Password = passwordDb;
newUser.Email = email;


if (editUser.Postcode != innUser.Postcode)
{
Debug.WriteLine("Inside in Postcode");
// Postcode is altered. First check if the new postcode is in the tabel already
City existingCity = db.Cities.Find(innUser.Postcode);
if (existingCity == null)
{
Debug.WriteLine("Inside in not exsisting postcode");
// poststedet eksisterer ikke, må legges inn
var newCity = new City()
{
Postcode = innUser.Postcode,
Cityname = innUser.city.Cityname
};
db.Cities.Add(newCity);
}
else
{ // city with the new postcode already exists, changing only postcode of user
editUser.Postcode = innUser.Postcode;
Debug.WriteLine("Inside in exsisting postcode");
}
};
Debug.WriteLine("Right before save");
db.SaveChanges();
Debug.WriteLine("Returning true");
return true;
}
catch
{
Debug.WriteLine("returning false");
return false;
}
}

捕获异常的输出:

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at Oblig1.DBUser.editUser(String email, User innUser) in c:\Users\XXX\XXX\XXX\XXX\DBUser.cs:line 55

错误输出:

A first chance exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll
Entity of type "City" in state "Added" has the following validation errors:
- Property: "Postcode", Error: "Postcode must be added"
Entity of type "User_546F9926F8DC53E2EF66BA48BE431DF1B26DEBEFA54B0597E60AEB1839DD022C" in state "Modified" has the following validation errors:
- Property: "city", Error: "The city field is required."

我使用三个表的模型:

public class User
{
[Required(ErrorMessage = "Firstname must be added")]
public string Firstname { get; set; }

[Required(ErrorMessage = "Surname must be added")]
public string Surname { get; set; }

[Required(ErrorMessage = "Address must be added")]
public string Address { get; set; }

public string Postcode { get; set; }

[Key]
[Required(ErrorMessage = "E-mail must be added")]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }

[Required(ErrorMessage = "Phonenr must be added")]
public string Phonenr { get; set; }

[Required(ErrorMessage = "Password must be added")]
public string Password { get; set; }

[Required]
public virtual City city { get; set; }

public virtual List<Order> Orders { get; set; }
}

public class dbUser
{
[Key]
public string Email { get; set; }
public byte[] Password { get; set; }
}

public class City
{
[Key]
[Required(ErrorMessage = "Postcode must be added")]
public string Postcode { get; set; }

[Required(ErrorMessage = "City must be added")]
public string Cityname { get; set; }
}

最佳答案

当您创建新的城市对象时,您可以这样做:

var newCity = new City()
{
Postcode = innUser.Postcode,
Cityname = innUser.city.Cityname
};

相反,您需要使用 City.Postcode 属性设置邮政编码,如下所示:

var newCity = new City()
{
Postcode = innUser.city.Postcode,
Cityname = innUser.city.Cityname
};

关于c# - dbContext.SaveChanges() 不保存也不输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26301814/

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