5

Đó là lần đầu tiên tôi sử dụng asp 5 \ core1 và tôi đang gặp vấn đề thiết lập một khuôn khổ thực thể dbcontext'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1 [TContext]' vi phạm chế kiểu tham số 'TContext'

tôi có một thư viện lớp học với đối tượng của tôi

public class Utilizador 
    { 
     public Utilizador() 
     { 

     } 

     public int id { get; set; } 
    } 

Sau đó, tôi có một dự án Web Api với một tham chiếu đến lớp học của tôi và một bối cảnh

public class Context : DbContext 
{ 
    public Context(DbContextOptions<Context> options) 
     : base(options) 
    { 

    } 

    public DbSet<Utilizador> Utilizadores { get; set; } 

} 

package.json My Lik của nó e này liên quan đến thực thể khuôn khổ

"dependencies": { 
    "Microsoft.NETCore.App": { 
     "version": "1.0.0-rc2-3002702", 
     "type": "platform" 
    }, 
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.Mvc.WebApiCompatShim": "1.0.0-rc2-final", 
    "Microsoft.EntityFrameworkCore": "1.0.0-rc2-final", 
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview1-final" , 
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final" 
    }, 

    "tools": { 
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": { 
     "version": "1.0.0-preview1-final", 
     "imports": "portable-net45+win8+dnxcore50" 
    }, 
    "Microsoft.EntityFrameworkCore.Tools": { 
    "version": "1.0.0-preview1-final", 
    "imports": [ 
     "portable-net45+win8+dnxcore50", 
     "portable-net45+win8" 
    ] 
    } 
    }, 

    "frameworks": { 
    "netcoreapp1.0": { 
     "imports": [ 
     "dotnet5.6", 
     "dnxcore50", 
     "portable-net45+win8" 
     ] 
    } 
    } 

Cuối cùng startup.cs tôi

  public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddMvc(); 
     services.AddEntityFramework().AddEntityFrameworkSqlServer().AddDbContext<Context>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"])); 

    } 

tôi đã sử dụng lệnh Add-Migrations và tạo ra cơ sở dữ liệu nó tôi và một bảng gọi là __MigrationsHistory nhưng didnt tạo nào cho các lớp học của tôi vì vậy i sử dụng Add-Migration "mycontext" và tất cả mọi thứ ngừng làm việc, bây giờ mọi tôi cố gắng để làm một sự chuyển đổi tôi nhận được lỗi này:

System.ArgumentException: GenericArguments[0], 'WebApiSolution.Migrations.Context', on 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type 'TContext'. ---> System.TypeLoadException: GenericArguments[0], 'WebApiSolution.Migrations.Context', on 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type parameter 'TContext'. at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type) 
    at System.RuntimeTypeHandle.Instantiate(Type[] inst) 
    at System.RuntimeType.MakeGenericType(Type[] instantiation) 
    --- End of inner exception stack trace --- 
    at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e) 
    at System.RuntimeType.MakeGenericType(Type[] instantiation) 
    at Microsoft.EntityFrameworkCore.Design.DbContextOperations.FindContextFactory(Type contextType) 
    at Microsoft.EntityFrameworkCore.Design.DbContextOperations.FindContextTypes() 
    at Microsoft.EntityFrameworkCore.Design.DbContextOperations.FindContextType(String name) 
    at Microsoft.EntityFrameworkCore.Design.DbContextOperations.CreateContext(String contextType) 
    at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.RemoveMigration(String contextType, Boolean force) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsRemoveCommand.<>c__DisplayClass0_0.<Configure>b__0() 
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) 
GenericArguments[0], 'WebApiSolution.Migrations.Context', on 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type 'TContext'. 

ai đó có thể xin vui lòng chỉ tôi đi đúng hướng? tôi đang làm gì sai, tôi đang thiếu gì ở đây?

nhờ

Trả lời

0

Hãy thử thêm một initializer cơ sở dữ liệu ngữ cảnh của bạn:

Edit: câu trả lời đầu tiên của tôi được dựa trên Entity Framework 6. Database.EnsureCreated() âm thanh như nó có thể là phương pháp tương đương mới nhất phát hành:

public Context(DbContextOptions<Context> options) 
     : base(options) 
    { 
     Database.EnsureCreated(); 
    } 
+0

không làm việc thậm chí không tìm thấy những phương pháp đó cho cơ sở dữ liệu, tôi biết có một số thay đổi khá lớn trong phiên bản trước và khó tìm thấy ppl sử dụng nó ...:/ – Shakawkaw

+0

các gói phụ thuộc một nd công cụ, tôi redid nó và thêm một số l, khởi động lại vs và bây giờ nó hoạt động ... có thể là một tài liệu tham khảo xấu hoặc một cái gì đó là vấn đề ... nếu cần thiết sẽ đăng những thay đổi sau ... thanks – Shakawkaw

+1

@Shakawkaw chính xác điều gì đã khắc phục được sự cố của bạn? Tôi có cùng một cái. Theo một hướng dẫn để tìm hiểu vì vậy tôi không chắc chắn chính xác những gì là sai với các công cụ project.json và phụ thuộc của tôi. – Hank

Các vấn đề liên quan