1

왜 오류가 발생했는지는 분명하지 않습니다. 내가 처음 프로젝트를 비계 할 때 마이그레이션 및 업데이트 데이터베이스 명령은 잘 돌아 갔지만 응용 프로그램을 거의 변경하지 않은 후에는 this error이되었습니다. 주위에 떠있는 유일한 해결책은 this입니다 :'TContext'에 매개 변수없는 생성자가 없습니다.

public class BloggingContextFactory : IDbContextFactory<BloggingContext> 
{ 
    public BloggingContext Create() 
    { 
     var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>(); 
     optionsBuilder.UseSqlite("Data Source=blog.db"); 

     return new BloggingContext(optionsBuilder.Options); 
    } 
} 

내 DbContext :

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IApplicationDbContext 
{ 
    public DbSet<ApplicationUserCode> ApplicationUserCodes { get; set; } 

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 
     : base(options) 
    { 
    } 

    protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
     // Customize the ASP.NET Identity model and override the defaults if needed. 
     // For example, you can rename the ASP.NET Identity table names and more. 
     // Add your customizations after calling base.OnModelCreating(builder); 
    } 
} 

그것은 나를 위해 작동하지만 연결 문자열을 하드 코딩 나와 함께 잘되지 않습니다 클래스 안에 있습니다. 이 오류가 한 번에 발생하지 않은 이유에 대한 명확한 설명은 클래스에 연결 문자열을 포함시키는 것보다 훌륭한 솔루션이며 또한 훌륭한 솔루션입니다.

답변

2

는이

public class BloggingContext : DbContext 
{ 
    public BloggingContext(){   // << The reason.... 

    } 
    public BloggingContext(DbContextOptions<BloggingContext> options) 
     : base(options) 
    { } 

    public DbSet<Blog> Blogs { get; set; } 
} 

또 다른 이유는이 방법 자체의 상단에있는 OnModelCreating(){} 방법 내부 base.OnModelCreating(builder); 호출없이 OnModelCreating(ModelBuilder builder){} 재정의를보고 있었다이다 가지고 있었다. 왜 저를 던져 주 었는지 ... 저도 몇 가지를 시도해 보았습니다. 그리고 저주받은 매개 변수없는 생성자를 간단하게 포함시킨 것이 범인이었습니다.

protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); //<< Absolutely required at the top. 
     // Customize the ASP.NET Identity model and override the defaults if needed. 
     // For example, you can rename the ASP.NET Identity table names and more. 
     // Add your customizations after calling base.OnModelCreating(builder); 

편집

//Startup.cs ConfigureServices (IServiceCollection 서비스)

//appsettings.json

{ 
    "ConnectionStrings": { 
    "DefaultConnection": "Data Source=(LocalDb)\\MssqlLocalDb;Initial  Catalog=PilotSystemCore;Integrated Security=True", 
"SqliteConnection" : "Data Source=SqliteDbName.db" 
}, 
"Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
     "Default": "Debug", 
     "System": "Information", 
     "Microsoft": "Information" 
    } 
} 
} 

NB

services.AddDbContext<ApplicationDbContext>(options => 
     options.UseSqlite(Configuration.GetConnectionString("SqliteConnection"))); 
: 어떤 마이그레이션이 한 경우 이전에 SqlServer를 사용하여 생성 된 항목은 유효 기간이 긴 다시 실행 마이그레이션.

+0

내 게시물을 DbContext의 모양으로 업데이트했습니다. – lbrahim

+0

내 StartUp 및 DbContext 코드 [여기] (https://github.com/aspnet/EntityFramework/issues/7641#issuecomment-290339592)를 찾을 수 있습니다. – lbrahim

관련 문제