저는 같은 문제에 직면하고 있습니다. 해결책은 구성 자에서 시드 이벤트를 노출하고 응용 프로그램에 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 데이터를 시드하기 위해 구독합니다.
SimpleMembership을 도메인에서 사용하게하려면 몇 가지 트릭이 있지만 유용한 답변을 얻으려면 자세한 정보를 공유해야합니다. 귀하의 도메인은 사용자 회원 정보와 밀접하게 통합되어 있습니까? 아니면 완전히 분리되어 있습니까? OAth 사용을 계획하고 있습니까? 너는 무엇을 뿌리는데 얼마나 중요한가? 시딩은 단지 단위 테스트 용입니까, 아니면 생산 용입니까? 제 생각에는 회원 정보와 역할 정보에 필요한 확장과 통합이 많이 필요한 경우 맞춤 멤버십과 역할 제공 업체를 만드는 것이 좋습니다. –