1

When I use update-database –verbose after adding migration, I got this error

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

I read 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type parameter 'TContext' and 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser', but there isn't any answer in those questions that I could see. I've used ASP.NET Core 2 and 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; }
}

and

   {
  "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"
    }
  }
}

and startup.cs

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

and entityConfiguration

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

and migration snapshot

 [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");
            });
    }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Amin Saadati
  • 719
  • 8
  • 21

1 Answers1

1

I could do it with @DalmTo help. I could make a new database with add and update migration in EF core and dot net core. After read this Link that @DalmTo gave me, Frist I wrote Add-Migration -Name "MyCoreMigration" and after create migration files I applied in database with this line update-database –verbose. Problem was in Name "MyCoreMigration" in Add-Migration -Name "MyCoreMigration" before this I used Add-Migration.So you need to set a name for migration as -Name "MyCoreMigration

Amin Saadati
  • 719
  • 8
  • 21