有没有一种方法,以消除内MVC 4实体框架默认的相关性?
有没有办法删除一个ASP.NET MVC 4项目中的实体框架默认的依赖,并有其他类似的技术取代它如的小巧玲珑
Is there a way to remove the default dependency on Entity Framework within an ASP.NET MVC 4 project and replace it with another similar technology such as Dapper
如果你创建一个新的ASP.NET MVC 4 Web应用程序,然后选择Internet应用程序作为项目模板,你会发现,实体框架被引用并作为SimpleMembership的一部分。但是,只有在实体框架一个非常小的依赖,这可能是不值得删除它。
If you create a new ASP.NET MVC 4 Web Application and choose "Internet Application" as the project template you will notice that Entity Framework is referenced and used as part of SimpleMembership. However, there is only a very small dependency on Entity Framework and it probably isn't worth removing it.
实体框架似乎被用于仅2次要任务
在其中使用Entity Framework的第一种方式是创建存储会员数据所需的数据库架构。数据库模式创建不是那种小巧玲珑会为你做,如果你删除实体框架,你必须手动管理更改您的会员模型/数据库。
The first way in which Entity Framework is used is to create the database schema needed to store the membership data. Database schema creation is not something that Dapper will do for you and if you remove Entity Framework you will have to manually manage changes to your membership models/database.
在其中使用Entity Framework的第二种方法是名为ExternalLoginConfirmation默认的AccountController内作为其一部分的OAuth集成一个单独的方法中。它是用来注册已经从外部供应商(如Facebook)认证的新用户。
The second way in which Entity Framework is used is within a single method named "ExternalLoginConfirmation" inside the default "AccountController" as part of it's OAuth integration. It is used to register new users that have been authenticated from an external provider (like Facebook).
我们可以说那么SimpleMembership使用SQL命令,而不是实体框架[1]。
We can say then that SimpleMembership uses SQL commands and not Entity Framework [1].
由于SimpleMembership使用SQL命令,而不是实体框架应该尽可能快地完成这个任务,可比的小巧玲珑的解决方案。此外,SimpleMembership的这种配置已被广泛由微软和社区测试。由于这些原因我会离开单干。 code与安全涉及应交由认可的安全专家[2]。
Because SimpleMembership uses SQL commands and not Entity Framework it should be as fast as a comparable Dapper solution for this task. In addition, this configuration of SimpleMembership has been tested extensively by Microsoft and the community. For those reasons I would leave it alone. Code that deals with security should be left to accredited security experts [2].
如何从一个新的ASP.NET MVC 4 Web应用程序中删除实体框架
如果你真的想,依赖是很容易删除(我假设你的会员数据库已经创建和工作)。
If you really want to, the dependency is very easy to remove (I am assuming your membership database is already created and working).
=====> 第1步
在Visual Studio解决方案资源管理器中打开引用文件夹中。
In the Visual Studio Solution Explorer open up the "References" folder.
右键单击并选择删除。
=====> 第2步
开放的过滤器/ InitializeSimpleMembershipAttribute.cs 的
删除以下内容:
using System.Data.Entity.Infrastructure;
和
Database.SetInitializer<UsersContext>( null );
和
using ( var context = new UsersContext() ) {
if ( !context.Database.Exists() ) {
// Create the SimpleMembership database without Entity Framework migration schema
( ( IObjectContextAdapter ) context ).ObjectContext.CreateDatabase();
}
}
=====> 第3步
打开:型号/ AccountModels.cs 的
删除以下内容:
public class UsersContext : DbContext {
public UsersContext()
: base( "DefaultConnection" ) {
}
public DbSet<UserProfile> UserProfiles {
get;
set;
}
}
=====> 第四步
打开:控制器/ AccountController.cs 的
查找命名方法ExternalLoginConfirmation。
Look for the method named "ExternalLoginConfirmation".
替换以下内容:
using ( UsersContext db = new UsersContext() ) {
UserProfile user = db.UserProfiles.FirstOrDefault( u => u.UserName.ToLower() == model.UserName.ToLower() );
// Check if user already exists
if ( user == null ) {
// Insert name into the profile table
db.UserProfiles.Add( new UserProfile {
UserName = model.UserName
} );
db.SaveChanges();
通过您的短小精悍相当于code - 在code-意见告诉你需要实现什么
With your dapper equivalent code -- the code-comments tell you what needs to be implemented.
您应该能够完全消除这个问题和其他方法,如果你不使用OAuth的。
You should be able to remove this and other methods altogether if you are not using OAuth.
乙醚瞧;)
[1]一切都如SQL调用来执行,而不需要存储过程,视图,代理和更改的通知。结果Jon加洛韦,微软
[1] "Everything's implemented as SQL calls rather than requiring stored procedures, views, agents, and change notifications."
Jon Galloway, Microsoft
[2]让我给你滚动自己的密码算法和安全系统我所有的标准。注意:不要这是非常,非常容易创造出几乎但不完全安全的防盗系统的安全系统。它给你安全的错觉是比没有保障制度更糟糕了!结果Eric利珀特,联想
[2] "Let me give you all my standard caution about rolling your own cryptographic algorithms and security systems: don't. It is very, very easy to create security systems which are almost but not quite secure. A security system which gives you a false sense of security is worse than no security system at all!"
Eric Lippert, Legend