それは私の最初の時間は、asp 5 \ core1を使用していると私はエンティティのフレームワークdbcontextを設定する問題を抱えている
私はオブジェクトを持つ1つのクラスライブラリを持っています
public class Utilizador
{
public Utilizador()
{
}
public int id { get; set; }
}
それから私はクラスとコンテキストを参照してWeb APIプロジェクトを持っています
public class Context : DbContext
{
public Context(DbContextOptions<Context> options)
: base(options)
{
}
public DbSet<Utilizador> Utilizadores { get; set; }
}
私のpackage.jsonそのようなエンティティのフレームワークに関して
"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"
]
}
}
最後に私のstartup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddEntityFramework().AddEntityFrameworkSqlServer().AddDbContext<Context>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));
}
Add-Migrationsコマンドを使用して、データベースと__MigrationsHistoryという1つのテーブルを作成しましたが、私のクラスのために作成したものは作成しませんでしたので、追加マイグレーション "mycontext"を使用しました。エラー:
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'.
誰かが私を正しい方向に向けることができますか?私は間違って何をしています、私はここで何が欠けていますか?
ありがとう
コンテキストにデータベースイニシャライザを追加してみてください:
編集:私の最初の答えは、Entity Framework 6に基づいていました。Database.EnsureCreated()は、最新リリースの同等のメソッドであるように思えます。
public Context(DbContextOptions<Context> options)
: base(options)
{
Database.EnsureCreated();
}