使用SimpleMembership与EF模型 [英] Using SimpleMembership with EF model-first

查看:167
本文介绍了使用SimpleMembership与EF模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将 SimpleMembership EF model-first 一起使用吗?当我尝试它,当我调用 WebSecurity.InitializeDatabaseConnection时,我得到无法找到请求的.NET Framework数据提供者。

Can SimpleMembership be used with EF model-first? When I try it, I get "Unable to find the requested .NET Framework Data Provider" when I call WebSecurity.InitializeDatabaseConnection.

换句话说:当连接字符串使用 System时,我无法收到 WebSecurity.InitializeDatabaseConnection 的调用。 Data.EntityClient 提供者(如同使用 model-first 范例时一样)。

To put it another way: I can't get the call to WebSecurity.InitializeDatabaseConnection to work when the connection string employs the System.Data.EntityClient provider (as it does when using the model-first paradigm).

要重新发布问题,创建一个MVC 4应用程序,并使用 model-first 用户类替换代码优先 UserProfile实体类(您可以免费使用MVC 4模板)您已在实体设计器中创建:

To repro the issue, create an MVC 4 app, and replace the code-first UserProfile entity class (which you get for free with the MVC 4 template) with a model-first User class that you have created in the Entity Designer:


  1. VS 2012 中创建一个MVC 4应用程序,并添加一个新的,空白实体数据
    模型。

  2. 向模型添加名为用户的新实体,其中 Id,
    UserName和FullName 。所以,在这一点上, User 数据实体是
    映射到 Users 表,并通过一个时髦的连接
    字符串,使用 System.Data.EntityClient 提供者。

  3. 验证 EF 可以访问用户实体。一个简单的方法来做
    ,它是基于用户表
    及其关联的DbContext来构建一个用户控制器。

  4. 编辑 AccountModels.cs 文件删除 UserProfile 类和
    其关联的 UsersContext 类。替换引用
    (现在缺少) UserProfile UsersContext 类引用
    您的新用户类及其相关联的 DbContext 类。

  5. 将调用InitializeDatabaseConnection从
    InitializeSimpleMembershipAttribute过滤器类移动到
    Global.asax.cs中的Application_Start方法。在
    修改参数以使用您的新用户实体的连接
    字符串,表名和UserId列名。

  6. 删除(否更长的使用) InitializeSimpleMembershipAttribute
    类及其引用。

  1. Create an MVC 4 app in VS 2012 and add a new, blank Entity Data Model.
  2. Add a new Entity named User to the model, with fields for Id, UserName, and FullName. So, at this point, the User data entity is mapped to a Users table and is accessed via a funky connection string that employs the System.Data.EntityClient provider.
  3. Verify that the EF can access the User entity. One easy way to do that is to scaffold out a Users controller based on the User table and its associated DbContext.
  4. Edit the AccountModels.cs file to remove the UserProfile class and its associated UsersContext class. Replace the references to the (now missing) UserProfile and UsersContext classes with references to your new User class and its associated DbContext class.
  5. Move the call to InitializeDatabaseConnection from the InitializeSimpleMembershipAttribute filter class to the Application_Start method in Global.asax.cs. While you're at it, modify the arguments to use your new User entity's connection string, table name, and UserId column name.
  6. Delete the (no longer used) InitializeSimpleMembershipAttribute class and the references to it.

当您执行重命名时,它将在调用 InitializeDatabaseConnection。

When you run the repro, it will get an Exception at the call to InitializeDatabaseConnection.

Bob

推荐答案

SimpleMembership可以与模型一起工作。这是解决方案。

SimpleMembership can work with model first. Here is the solution.

1。 InitializeSimpleMembershipAttribute.cs 从MVC 4 Internet应用程序看起来应该像这样



1.InitializeSimpleMembershipAttribute.cs from MVC 4 Internet Application templete should look like this

namespace WebAndAPILayer.Filters
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
    {
        private static SimpleMembershipInitializer _initializer;
        private static object _initializerLock = new object();
        private static bool _isInitialized;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // Ensure ASP.NET Simple Membership is initialized only once per app start
            LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
        }

        private class SimpleMembershipInitializer
        {
            public SimpleMembershipInitializer()
            {
                try
                {
                    WebSecurity.InitializeDatabaseConnection("ConnStringForWebSecurity", "UserProfile", "Id", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("Something is wrong", ex);
                }
            }
        }
    }
}

2.从删除CodeFirst类AcountModel.cs

3.Fix AccountCotroler.cs 使用您的Model-first DbContext( ExternalLoginConfirmation(RegisterExternalLoginModel model,string returnUrl)方法)

3.Fix AccountCotroler.cs to work with your Model-first DbContext (ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) method)

4.定义您的ConnStringForWebSecurity连接字符串,与第一个数据库访问的funky conn字符串不同,请注意,我们使用提供程序 System.Data.SqlClient not System.Data.EntityClient

4.Define your "ConnStringForWebSecurity" connection string which is not same as that funky conn string for model-first db access, notice that we use provider System.Data.SqlClient not System.Data.EntityClient

 <connectionStrings>
         <add name="ModelFirstEntityFramework" connectionString="metadata=res://*/Context.csdl|res://*/Context.ssdl|res://*/Context.msl;provider=System.Data.SqlClient;provider
 connection string=&quot;data source=.\SQLEXPRESS;Initial
 Catalog=aspnet-MVC4;Integrated
 Security=SSPI;multipleactiveresultsets=True;App=EntityFramework&quot;"
 providerName="System.Data.EntityClient" />
         <add name="ConnStringForWebSecurity" connectionString="data source=.\SQLEXPRESS;Initial Catalog=aspnet-MVC4;Integrated
 Security=SSPI" providerName="System.Data.SqlClient" />
       </connectionStrings>

这篇关于使用SimpleMembership与EF模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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