【问题标题】:Entity framework migrations, domain outside mvc4, SimpleMembership实体框架迁移,mvc4 之外的域,SimpleMembership
【发布时间】:2013-01-11 11:42:21
【问题描述】:

我喜欢 MVC4 之外的域。因此,我的迁移文件夹位于域项目中。

和其他人一样,我想使用以下方法来播种由网络安全创建的表: http://odetocode.com/Blogs/scott/archive/2012/08/31/seeding-membership-amp-roles-in-asp-net-mvc-4.aspx

由 SimpleMembershipProvider 创建的表不会暴露给我的 dbContext 并且播种这些表的唯一方法是使用链接中的代码。显然,这段代码在我的域项目中不起作用。

使用迁移时在 MVC4 之外拥有域项目是否意味着我不能再使用 SimpleMembershipProvider?或者有没有一种巧妙的方法将这三件事结合在一起?

对不起,如果这是一个有点愚蠢的问题,因为我刚搬到 MVC4。谢谢

【问题讨论】:

  • 您可以采取一些技巧来使 SimpleMembership 与您的域一起使用,但您可能需要分享更多信息才能获得有用的答案。您的域是与用户成员信息紧密集成还是完全独立?您是否打算使用 OAth。你在播种什么,它有多重要?播种仅用于单元测试还是生产需要?在我看来,如果成员资格和角色信息需要大量扩展和集成,您最好创建自己的自定义成员资格和角色提供程序。

标签: asp.net-mvc-4 entity-framework-5 entity-framework-migrations simplemembership


【解决方案1】:

我面临同样的问题,我想出的解决方案是在配置器中公开一个种子事件,并让应用程序使用它订阅 SimpleMembership 种子。

首先,配置:

    internal sealed class Configuration : DbMigrationsConfiguration<MyContext>
    {
        public event EventHandler<MyContext> OnSeed; 

        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(MyContext context)
        {
            var onSeed = this.OnSeed;
            if (onSeed != null)
                onSeed(this, context);

            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }

然后,为了能够将它与 Database.SetInitializer 一起使用,我基于 MigrateDatabaseToLatestVersion 创建了自己的 IDatabaseInitializer。这很容易做到,因为正如 this post 所说(您可以查看 here),它只是 DbMigration 的一个包装器。

public class MyDatabaseInitializer : IDatabaseInitializer<MyContext>
{
    private readonly Configuration config;

    public event EventHandler<MyContext> OnSeed
    {
        add { if (this.config != null) this.config.OnSeed += value; }
        remove { if (this.config != null) this.config.OnSeed -= value; }
    }

    /// <summary>
    ///     Initializes a new instance of the MigrateDatabaseToLatestVersion class.
    /// </summary>
    public MyDatabaseInitializer()
    {
        this.config = new Configuration();
    }

    /// <summary>
    ///     Initializes a new instance of the MigrateDatabaseToLatestVersion class that will
    ///     use a specific connection string from the configuration file to connect to
    ///     the database to perform the migration.
    /// </summary>
    /// <param name="connectionStringName"> The name of the connection string to use for migration. </param>
    public MyDatabaseInitializer(string connectionStringName)
    {
        Contract.Requires(!string.IsNullOrWhiteSpace(connectionStringName));

        this.config = new Configuration
                      {
                          TargetDatabase = new DbConnectionInfo(connectionStringName)
                      };
    }

    /// <inheritdoc />
    public void InitializeDatabase(MyContext context)
    {
        var migrator = new DbMigrator(config);
        migrator.Update();
    }
}

在这个初始化程序中,我公开了 OnSeed 事件。在 App_Start 中,在 MVC 项目中,我订阅它以播种 SimpleMembership 数据。

var initializer = new PoipDatabaseInitializer();
initializer.OnSeed += (s, e) => SeedSecurity();
Database.SetInitializer(initializer);


private static void SeedSecurity()
    {
        WebSecurity.InitializeDatabaseConnection("MyDatabase", "UserProfile", "UserId", "UserName", autoCreateTables: false);

        if (!Roles.RoleExists("SysAdmin"))
            Roles.CreateRole("SysAdmin");

        if (!WebSecurity.UserExists("sysadmin"))
            WebSecurity.CreateUserAndAccount(
                "sysadmin",
                "password"
            );

        if (!Roles.GetRolesForUser("sysadmin").Contains("SysAdmin"))
            Roles.AddUsersToRoles(new[] { "sysadmin" }, new[] { "SysAdmin" });
    }

【讨论】:

  • 嗨亚瑟,感谢您查看此内容。我需要承认我找到了解决此问题的方法,但太忙(懒惰)无法更新此问题。我刚刚根据此链接重新创建了 SimpleMembership Schema(请注意,评论中给出了正确的模式)。至于你的解决方案,我不好说没关系。我会发布我的解决方案,6 个月后我会用更多的赞成票标记那个,你说什么?或者也许会有第三个?但是你的解决方案很有趣。
  • 正如我所说,我正面临这个问题,我刚刚想出了这个解决方案。我会观察它是如何工作的以及它的注意事项(如果有的话),因为该项目仍在开发中。我真的很想看看你的或其他可用的!我还在我的模型中包含了 SimpleMembership 表 (blog.spontaneouspublicity.com/…),但我决定让 SimpleMembership 使用适当的方法为其数据播种,因为我不确定它是否修改了预期表之外的任何内容。
猜你喜欢
  • 2015-05-11
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 2019-06-24
相关资源
最近更新 更多