最近,在别人博客的回复上看到讨论Entity Framework性能的问题,有人不知所以人云亦云。说Entity Framework很好,甚至超越ado.net!!!
真的无言以对,对这样的老兄。只能淡淡的微笑了之,就一菜鸟!
说说自己的事,老证明不要迷恋谁,而要实践了解才是正道!
正在写MVC5+Bootstrap+postgreSQL的应用程序,本打算使用Entity Framework,但是总感觉性能差(我的观点,不知道性能问题可以不解决,对于知道的性能差绝对不用),因此使用Npgsql来直接控制数据库。问题出现了,Identity使用Entity Framework。Google了一番,终于找到方案!
在微软的网站上有单独的MySQL存储的项目源码,拿来修改成PostgreSQL数据库的。
1.使用Attribute来实现实体类与数据库之间的Insert和update的SQL语句生成,直接执行SQL即可添加和删除。
原因:为了通用允许派生IdentityUser,而增加的。
2.选择DynamicBuilder<T>作为DataReader转换为List<T>的映射方案
3.测试过其他的转换方案,包括老赵的Fasterflect和其他的库。结果如下:
第一名:老赵Fasterflect文章里面提到的Dynamic库。
第二名:老赵的Fasterflect
当然了,他们都是缓存方式。
100万次相差20毫秒左右。I3上测试的。Release模式!但是不知道为啥在家的机器上I5+8G+win7 64位上发现他们差不多了。
在编写的过程中发现如下的问题:
注册用户的时候
...
var result1 = await UserManager.CreateAsync(user, "123C567&80xx");
...
经过反编译出他的代码
public virtual async Task<IdentityResult> CreateAsync(TUser user, string password)
{
this.ThrowIfDisposed();
IUserPasswordStore<TUser, TKey> passwordStore = this.GetPasswordStore();
if (user == null)
{
throw new ArgumentNullException("user");
}
if (password == null)
{
throw new ArgumentNullException("password");
}
IdentityResult identityResult = await this.UpdatePasswordInternal(passwordStore, user, password).WithCurrentCulture<IdentityResult>();
IdentityResult result;
if (!identityResult.Succeeded)
{
result = identityResult;
}
else
{
result = await this.CreateAsync(user).WithCurrentCulture<IdentityResult>();
}
return result;
}
internal async Task<IdentityResult> UpdatePasswordInternal(IUserPasswordStore<TUser, TKey> passwordStore, TUser user, string newPassword)
{
IdentityResult identityResult = await this.PasswordValidator.ValidateAsync(newPassword).WithCurrentCulture<IdentityResult>();
IdentityResult result;
if (!identityResult.Succeeded)
{
result = identityResult;
}
else
{
await passwordStore.SetPasswordHashAsync(user, this.PasswordHasher.HashPassword(newPassword)).WithCurrentCulture();
await this.UpdateSecurityStampInternal(user).WithCurrentCulture();
result = IdentityResult.Success;
}
return result;
}
internal async Task UpdateSecurityStampInternal(TUser user)
{
if (this.SupportsUserSecurityStamp)
{
await this.GetSecurityStore().SetSecurityStampAsync(user, UserManager<TUser, TKey>.NewSecurityStamp()).WithCurrentCulture();
}
}
在SetSecurityStampAsync函数中居然调用了UserTable.Update(TUser user)方法,这就糊涂了?
明明是添加用户为啥在添加的过程中居然执行更新用户呢,这当然不能成功,多余的操作。
这是我编写自己的存储是这样!没有看Identity + Entity framework的代码。
因此不要迷恋谁,实践+学习最重要!
可能本人菜,不知道他为啥这么设计!有理解请指教!