有没有办法删除MVC 4中对实体框架的默认依赖关系? [英] Is there a way to remove the default dependency on Entity Framework within MVC 4?

查看:199
本文介绍了有没有办法删除MVC 4中对实体框架的默认依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在ASP.NET MVC 4项目中删除对Entity Framework的默认依赖项,并将其替换为其他类似的技术,例如 Dapper

解决方案

如果您创建了一个新的ASP.NET MVC 4 Web应用程序,选择Internet应用程序作为项目模板,您将注意到实体框架被引用并用作SimpleMembership的一部分。但是,实体框架的依赖性很小,可能不值得删除。



实体框架似乎只用于2个小任务



使用Entity Framework的第一种方式是创建存储成员数据所需的数据库模式。数据库模式创建不是Dapper为您做的,如果您删除Entity Framework,则必须手动管理对成员模型/数据库的更改。



第二种方式作为OAuth集成的一部分,使用了Entity Framework的默认AccountController内的一个名为ExternalLoginConfirmation的方法。它用于注册已从外部提供商(如Facebook)进行身份验证的新用户。



我们可以说SimpleMembership使用SQL命令而不是实体框架[1 ]。



由于SimpleMembership使用SQL命令,而不是Entity Framework,它应该与此任务的可比较Dapper解决方案一样快。此外,SimpleMembership的这种配置已被Microsoft和社区广泛测试。因为这些原因,我会离开它。处理安全性的代码应该留给认证的安全专家[2]。



如何从新的ASP.NET MVC 4 Web应用程序中删除Entity Framework



如果你真的想,依赖关系很容易删除(我假设你的会员数据库已经被创建和工作)。



=====> 步骤1



在Visual Studio解决方案资源管理器中打开参考文件夹。



右键单击EntityFramework并选择删除。



=== ==> 步骤2



打开过滤器/ InitializeSimpleMembershipAttribute.cs



删除以下内容:

 使用System.Data.Entity.Infrastructure; 

  Database.SetInitializer< UsersContext>(null); 

  using(var context = new UsersContext()){
if(!context.Database.Exists()){
//创建没有Entity Framework迁移架构的SimpleMembership数据库
((IObjectContextAdapter)上下文).ObjectContext.CreateDatabase();
}
}

=====> 步骤3



打开:模型/ AccountModels.cs



删除以下:

  public class UsersContext:DbContext {
public UsersContext()
:base(DefaultConnection ){
}

public DbSet< UserProfile> UserProfiles {
get;
设置;
}
}

=====> 步骤4



打开:控制器/ AccountController.cs



名为ExternalLoginConfirmation的方法。



替换以下内容:

  using(UsersContext db = new UsersContext()){
UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower()== model.UserName.ToLower());
//检查用户是否已经存在
if(user == null){
//将名称插入配置文件表
db.UserProfiles.Add(new UserProfile {
UserName = model.UserName
});
db.SaveChanges();

使用dapper等效代码 - 代码注释告诉你需要实现什么。 p>

如果您不使用OAuth,您应该可以完全删除此方法。



Et Voila;)


[1]一切都以SQL调用实现,而不是要求存储过程,视图,代理和更改通知。
< a href =http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web- form-and-asp-net-mvc-4-templates.aspx> Jon Galloway,Microsoft



[2]让我给你所有的关于滚动您自己的加密算法和安全系统的标准注意事项:不要创建几乎不是很安全的安全系统是非常非常容易的。给你一个错误的安全感的安全系统比没有安全性更差系统!
Eric Lippert,Legend



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

解决方案

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.

Entity Framework appears to be used for just 2 minor tasks

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.

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).

We can say then that SimpleMembership uses SQL commands and not Entity Framework [1].

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].

How to remove Entity Framework from a new ASP.NET MVC 4 Web Application

If you really want to, the dependency is very easy to remove (I am assuming your membership database is already created and working).

=====> Step 1

In the Visual Studio Solution Explorer open up the "References" folder.

Right-click on "EntityFramework" and select "Remove".

=====> Step 2

Open Filters/InitializeSimpleMembershipAttribute.cs

Delete the following:

using System.Data.Entity.Infrastructure;

and

Database.SetInitializer<UsersContext>( null );

and

using ( var context = new UsersContext() ) {
    if ( !context.Database.Exists() ) {
        // Create the SimpleMembership database without Entity Framework migration schema
        ( ( IObjectContextAdapter ) context ).ObjectContext.CreateDatabase();
    }
}

=====> Step 3

Open: Models/AccountModels.cs

Delete the following:

public class UsersContext : DbContext {
    public UsersContext()
        : base( "DefaultConnection" ) {
    }

    public DbSet<UserProfile> UserProfiles {
        get;
        set;
    }
}

=====> Step 4

Open: Controllers/AccountController.cs

Look for the method named "ExternalLoginConfirmation".

Replace the following:

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();

With your dapper equivalent code -- the code-comments tell you what needs to be implemented.

You should be able to remove this and other methods altogether if you are not using OAuth.

Et Voila ;)

[1] "Everything's implemented as SQL calls rather than requiring stored procedures, views, agents, and change notifications."
Jon Galloway, Microsoft

[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

这篇关于有没有办法删除MVC 4中对实体框架的默认依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆