【问题标题】:Why does using 'update-database –verbose' return a 'violates the constraint of type 'TContext'' error? [duplicate]为什么使用'update-database –verbose'会返回'违反'TContext'类型的约束'错误? [复制]
【发布时间】:2018-09-25 20:01:26
【问题描述】:

当我在添加迁移后使用update-database –verbose 时,出现此错误

GenericArguments[0], 'DataAccessLayer.Migrations.CoreDbContext', on 'Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory`1[TContext]' 违反了 'TContext' 类型的约束。

我阅读了'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type parameter 'TContext''Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser',但我看不到这些问题的任何答案。我用过 ASP.NET Core 2 和 EF Core。

 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>();
    }

和实体配置

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");
            });
    }
}

【问题讨论】:

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


【解决方案1】:

我可以在 @DalmTo 的帮助下做到这一点。我可以通过在 EF 核心和点网核心中添加和更新迁移来创建一个新数据库。 在阅读了@DalmTo 给我的Link 之后,我首先写了Add-Migration -Name "MyCoreMigration",然后在创建迁移文件后,我使用这一行update-database –verbose 在数据库中应用。问题出在Add-Migration -Name "MyCoreMigration" 中的名称“MyCoreMigration”中,在此之前我使用了 Add-Migration。因此您需要为迁移设置一个名称为 -Name "MyCoreMigration

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 2018-10-04
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多