在dot net core和entity framework core中,当我使用update-database -verbose时,我得到的这个错误违反了'TContext'类型的约束

时间:2018-04-16 06:57:54

标签: c# .net asp.net-core-2.0 core asp.net-core-webapi

当我在添加迁移后使用update-database -verbose时,我收到了此错误

 GenericArguments[0], 'DataAccessLayer.Migrations.CoreDbContext', on 'Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory`1[TContext]' violates the constraint of type 'TContext'.

我看到了linklink,但这些链接中没有任何答案。 我已经使用了asp.net核心2和EF核心,我真的无法理解上面的错误,如果我需要看到的所有关于我的项目中的错误(作为databasecontext或等),如果你想要我解决这个问题我会放在这里。

更新:

 public class CoreDbContext: DbContext
{
    public CoreDbContext(DbContextOptions options) : base(options)
    {

      //  Database.EnsureCreated(); // for create database
      //  Database.Migrate(); // for check migrate

    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        new StudentMap(modelBuilder.Entity<Student>());
    }
    public virtual DbSet<Student> Students { get; set; }
}

   {
  "ConnectionStrings": {
    //"DataBaseContext": "server=(LocalDB)\\MSSQLLocalDB;Persist Security Info=True;Initial Catalog=DataLayer.Context.DataBaseContext;Connection Timeout=30"
    "DataBaseContext": "Server=(localdb)\\mssqllocaldb;Database=CoreDbContext;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

和startup.cs

  public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<CoreDbContext>(options =>
 options.UseSqlServer(Configuration.GetConnectionString("DataBaseContext")));
        services.AddTransient<IStudentRepository, StudentRepository>();
    }

和entityConfiguration

public class StudentMap
{
    public StudentMap(EntityTypeBuilder<Student> entityBuilder)
    {
        entityBuilder.HasKey(t => t.Id);
        entityBuilder.Property(t => t.FirstName);
        entityBuilder.Property(t => t.LastName);
    }
}

和迁移快照

 [DbContext(typeof(CoreDbContext))]
partial class CoreDbContextModelSnapshot : ModelSnapshot
{
    protected override void BuildModel(ModelBuilder modelBuilder)
    {
        modelBuilder
            .HasAnnotation("ProductVersion", "2.0.2-rtm-10011")
            .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

        modelBuilder.Entity("DataAccessLayer.DatabaseContext.Entities.Student", b =>
            {
                b.Property<int>("Id")
                    .ValueGeneratedOnAdd();

                b.Property<string>("FirstName");

                b.Property<bool>("IsActive");

                b.Property<string>("LastName");

                b.HasKey("Id");

                b.ToTable("Students");
            });
    }
}

1 个答案:

答案 0 :(得分:0)

我可以用@DalmTo帮助完成。我可以创建一个新的数据库,在EF核心和点网核心中添加和更新迁移。 在阅读了@DalmTo给我的Link之后,Frist我写了Add-Migration -Name "MyCoreMigration"并在创建了迁移文件后,我在数据库中应用了这一行update-database –verbose。问题出现在姓名&#34; MyCoreMigration&#34;在Add-Migration -Name "MyCoreMigration"之前,我使用了Add-Migration。所以你需要为迁移设置一个名称为-Name&#34; MyCoreMigration