Using SimpleMembership with EF model-first

后端 未结 5 1863
孤城傲影
孤城傲影 2020-12-02 19:15

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

5条回答
  •  猫巷女王i
    2020-12-02 19:41

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

    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.Delete CodeFirst Classes from AcountModel.cs

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

    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

     
             
             
           
    

提交回复
热议问题