1

내 프로젝트에서 종자 데이터를 초기화하는 방법 (2 엔티티 프레임 워크 프로젝트) 2 DbContext 있습니다. 그렇다면 Module1DbContext에서이 테이블에 시드 데이터를 삽입 할 수 있습니까? CoreDbContext를 Module1DbContext의 Seed 메서드에 전달하려고 시도했지만 작동했습니다.어떻게 다른 DbContext

+0

이 EF 코어 또는 EF 6입니까? EF 코어의 경우 [여기] (https://blogs.msdn.microsoft.com/dotnet/2016/09/29/implementing-seeding-custom-conventions-and-interceptors-in-ef-core-1-0)를 참조하십시오. /) –

+0

이것은 EF 6, 형제 –

답변

1

예. 다른 컨텍스트를 새로 시작하고 필요한 것을하십시오. CoreDbContext가 다른 프로젝트에 있으면이를 참조해야합니다.

protected override void Seed(Module1DbContext context) 
{ 
    // get some data from the current context you want to use for seeding 
    var someItemFromM1 = context.FooBar.FirstOrDefault(fb => fb.Id == myID); 
    if (someDataFromM1 != null) 
    { 
     using (var coreContext = new CoreDbContext()) 
     { 
      // Using AddOrUpdate which is designed for seeding, but you could just use standard update code 
      coreContext.SystemStatuses.AddOrUpdate(
       ss => ss.Code, // Unique field to check so duplicate not added 
       new SystemStatus 
       { 
        Code = someItemFromM1.Code, 
        Description = someItemFromM1.Description 
       }); 
      coreContext.SaveChanges(); 
     } 
    } 
} 
+0

입니다. 감사합니다, 스티브! 나는 그것에 대해 생각하지 않았다, 하하 –