1

我创建了一个 mvc 4 应用程序代码第一个应用程序,成员资格表没有存储在 .\SQLEXPRESS 数据库中,我将连接字符串名称的名称更改为我的 dbcontext,即 EntitySets,并使用 EntitySet 上下文更改了 InitializeMembershipAttribute 类但它们仍然没有被存储?谢谢您的帮助!!

数据库上下文

 public class EntitySets: DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<UserStats> UserStats {get;set;}
    public DbSet<UserProfile> UserProfiles { get; set; }
}

账户模型

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

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

初始化会员

 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()
        {
            Database.SetInitializer<EntitySets>(null);

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

                WebSecurity.InitializeDatabaseConnection("EntitySets", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
            }
        }
    }

连接字符串

 <add name="EntitySets"  connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Fitness_Friend.Web.Models.EntitySets;Integrated Security=True" providerName="System.Data.SqlClient" />
4

1 回答 1

1

您必须使用 aspnet_regsql 工具手动注册 MembershipProvider。

有关此配置的分步配置,请参见 codeproject:http: //www.codeproject.com/Articles/89029/Using-SQL-Membership-with-ASP-NET-application

于 2013-08-10T20:10:41.253 回答