[英]Thoroughly Check to see if an Email Exists, NHibernate, LINQ
對於我的身份驗證模型,我想確保同一電子郵件不能被多次注冊。 我對nHibernate和部分LINQ都是新手,所以我問這是否足夠進行“檢查”以確保發生這種情況。
public MembershipCreateStatus CreateUser(string email, string password)
{
if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "userName");
if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var members = session.CreateCriteria<Member>().List<Member>();
// determine is the email address already exists in the database
if (members.Any(i => i.Email == email))
return MembershipCreateStatus.DuplicateEmail;
// create the new member, if they are valid
var member = new Member { Email = email };
session.SaveOrUpdate(member);
transaction.Commit();
}
}
return MembershipCreateStatus.Success;
}
有沒有更聰明的方式來做到這一點? 過去,我在以前的程序中遇到了麻煩(我在該程序上使用Linq to SQL),所以這次我想獲得一些專家建議。
您正在執行的操作是加載所有成員並查詢內存中的集合。
這將在數據庫中執行查詢:
//this of course goes inside the transaction
if session.Query<Member>().Any(i => i.Email == email))
return MembershipCreateStatus.DuplicateEmail;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.