-
카테고리
-
세부 분야
백엔드
-
해결 여부
해결됨
개체 이름 'Teachers'이(가) 유효하지 않습니다.
19.02.08 14:11 작성 조회수 175
0
예외가 발생했는데 잡을 수가 없습니다.
예외 내용입니다. ===
System.AggregateException
HResult=0x80131500
메시지=One or more errors occurred. (개체 이름 'Teachers'이(가) 유효하지 않습니다.)
소스=System.Private.CoreLib
StackTrace:
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at FirstApp.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, DbSeeder seeder) in X:1.Git1.SourceUTILFirstAppFirstAppStartup.cs:line 51
내부 예외 1:
SqlException: 개체 이름 'Teachers'이(가) 유효하지 않습니다.
다음은 에외가 발생한 소스 코드입니다. ===
namespace FirstApp.Data{
public class DbSeeder
{
private FirstAppContext _context;
public DbSeeder(FirstAppContext context)
{
_context = context;
}
public async Task SeedDatabase()
{
if(!_context.Teachers.Any()) ===> 에러 발생 위치
{
List<Teacher> teachers = new List<Teacher>()
{
new Teacher(){Name = "세종대왕", Class="한글"},
new Teacher(){Name = "이순신", Class="해상전략"},
new Teacher(){Name = "제갈량", Class="지략"},
new Teacher(){Name = "을지문덕", Class="지상전략"},
};
await _context.AddRangeAsync(teachers);
// await _context.AddAsync(new Teacher() {Name = "세종대왕", Class = "한글"});
await _context.SaveChangesAsync();
}
}
}
}
namespace FirstApp.Data
{
public class FirstAppContext : DbContext
{
public FirstAppContext(DbContextOptions options) : base(options){}
public DbSet<Student> Students { get; set; }
public DbSet<Teacher> Teachers { get; set; }
}
}
강좌를 돌려보며 몇번을 확인했지만 다른 부분을 찾을 수가 없습니다.
if(!_context.Teachers.Any())
부분에서 에러가 발생하고 호출한 위치로 이동합니다.
답변을 작성해보세요.
1
SEJONG IT EDU
지식공유자2019.02.09
https://drive.google.com/file/d/1Goz0LT28FiPJvPZZliqXhc8Ks4xD9yYv/view
여기서 완성된 프로젝트 다운받으셔서 실행시키시고 기존에 있는 DB 삭제하시기 바랍니다.
그리고 다운받은 프로젝트의 Startup.cs에서
seeder.SeedDatabase().Wait();
이 부분 잠시 주석처리하시구요. // seeder.SeedDatabase().Wait();
cmd 여셔서 업데이트 마이그레이션 커맨드 실행합니다.
dotnet ef database update
그 후 다시 Startup.cs파일로 가셔서 주석처리한 부분 다시 복구시키고 앱 실행해보세요.
답변 1