1

MVC4 외부의 도메인을 갖고있는 것이 좋습니다. 따라서 내 마이그레이션 폴더는 도메인 프로젝트에 있습니다.엔티티 프레임 워크 마이 그 레이션, mvc4, SimpleMembership 외부 도메인

다른 사람으로 내가 사용하는 웹 보안에 의해 만들어진 테이블을 시드 싶습니다 SimpleMembershipProvider 만든 http://odetocode.com/Blogs/scott/archive/2012/08/31/seeding-membership-amp-roles-in-asp-net-mvc-4.aspx

테이블을 내 dbContext 이러한 테이블 시드의 유일한 방법에 노출되지 않는 것은의 코드를 사용하는 것입니다 링크. 분명히이 코드는 내 도메인 프로젝트에서 작동하지 않습니다.

마이그레이션을 사용할 때 MVC4 외부에서 도메인 프로젝트를 사용하면 SimpleMembershipProvider를 더 이상 사용할 수 없다는 의미입니까? 아니면이 세 가지를 결합하는 깔끔한 방법이 있습니까?

미안 MVC4로 옮겨 가면서 바보 같은 질문을하는 경우 미안합니다. 감사합니다.

+0

SimpleMembership을 도메인에서 사용하게하려면 몇 가지 트릭이 있지만 유용한 답변을 얻으려면 자세한 정보를 공유해야합니다. 귀하의 도메인은 사용자 회원 정보와 밀접하게 통합되어 있습니까? 아니면 완전히 분리되어 있습니까? OAth 사용을 계획하고 있습니까? 너는 무엇을 뿌리는데 얼마나 중요한가? 시딩은 단지 단위 테스트 용입니까, 아니면 생산 용입니까? 제 생각에는 회원 정보와 역할 정보에 필요한 확장과 통합이 많이 필요한 경우 맞춤 멤버십과 역할 제공 업체를 만드는 것이 좋습니다. –

답변

0

저는 같은 문제에 직면하고 있습니다. 해결책은 구성 자에서 시드 이벤트를 노출하고 응용 프로그램에 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 데이터를 시드하기 위해 구독합니다.

+0

안녕하세요 아서, 이것을 보아 주셔서 감사합니다. 나는이 문제에 대한 해결책을 찾았지만이를 업데이트하기에는 너무 바빴다는 것을 인정할 필요가있다. 이 링크마다 SimpleMembership Schema를 다시 만들었습니다 (주석에 정확한 스키마가 있음을 기억하십시오). 당신의 해결책에 관해서는, 나는 그것이 충분하다고 말할만큼 좋지 않습니다. 나는 나의 해결책을 게시 할 것이고 6 개월 후에 나는 더 많은 upvotes가있는 것을 표시 할 것이다, 당신은 무엇을 말합니까? 아니면 세 번째 것이있을 것인가? 하지만 당신의 솔루션은 매우 흥미 롭습니다. –

+0

내가 말했듯이 나는이 문제에 직면하고 있으며 나는이 해결책을 찾았다. 프로젝트가 아직 개발 중일 때 어떻게 작동되는지와 그 경고가 있는지를 살펴볼 것입니다. 나는 너의 것 또는 가능한 다른 것들을 살펴보고 싶다. 내 모델에도 SimpleMembership 테이블이 포함되어 있습니다 (http://blog.spontaneouspublicity.com/including-asp-net-simple-membership-tables-as-part-of-your-entity-framework-model). SimpleMembership이 적절한 메소드를 사용하여 데이터를 시드하도록합니다. 예상되는 테이블 이외의 다른 것을 수정하는지 확신 할 수 없습니다. –