gpt4 book ai didi

asp.net-mvc - 添加到数据库的重复用户

转载 作者:行者123 更新时间:2023-12-02 22:00:23 25 4
gpt4 key购买 nike

我尝试向我的数据库中添加一个新值。 UserPasswordRePassword 必须具有相同的值,并且数据库中必须不存在具有 UserName 的用户。

  public User NewUser(int HotelID, string UserName, string UserPassword, string RePassword, string FullName, string Email, bool Active, bool MasterUser)
{
User user = new User();
user.HotelID = HotelID;
user.UserName = UserName;
user.UserPassword = UserPassword;
user.FullName = FullName;
user.Email = Email;
user.IsActiveq = Active;
user.IsMaster = MasterUser;

var cekUser = (from c in _UserRepository.All()
where c.HotelID == HotelID
select c.UserName).ToList();
if (UserPassword == RePassword)
{
foreach (string cek in cekUser)
{
var x = cek;
if (UserName != x)
{
_UserRepository.Add(user);
}
}
}

_UserRepository.CommitChanges();

return user;

}

每次我运行我的代码时,都会向数据库中添加一个新行,尽管数据库中已经存在具有所提供用户名的用户。

为什么会这样?我的代码的哪一部分是错误的?

最佳答案

我认为你的代码应该是这样的:

if (UserPassword == RePassword)
{
// Also I thinks you should finish whether user existed logic in database
// but for now, let's follow your original logic
var existedUsers = (from c in _UserRepository.All()
where c.HotelID == HotelID
select c.UserName).ToList();

if (!existedUsers.Any(u => u == UserName))
{
_UserRepository.Add(user);
_UserRepository.CommitChanges();
}
}

关于asp.net-mvc - 添加到数据库的重复用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17056607/

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