在 asp.net core 3.1 中从我的主机复制数据库后再次迁移

Migration again after copy db from my host in asp.net core 3.1

我试图从服务器主机复制我的数据库及其行和所有数据。在我这样做之后,我尝试先迁移我的代码,迁移创建了另一个与您在此图像中看到的名称相同的表

所以有人可以解释发生了什么以及为什么要创建另一个表吗?以及我如何使用我的旧列及其数据?

我试图用空 up/down migratoin 文件进行迁移

这是我的本地连接字符串

Data Source=.;Initial Catalog=gasdwa7d;Integrated Security=True

和我的Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
        {
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultDbConnection"));
            options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
        });



        services.AddIdentity<ApplicationUser, IdentityRole>(options =>
        {
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireDigit = false;
            options.Password.RequireLowercase = false;
            options.Password.RequireUppercase = false;
            options.Password.RequiredLength = 6;
        })
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultUI()
            .AddDefaultTokenProviders();
        services.AddControllersWithViews();
        services.AddMvc(options => options.EnableEndpointRouting = false);
        services.AddRazorPages();
        services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>
            , ApplicationUserClaimsPrincipalFactory>();
        services.AddSignalR();

        services.Configure<ForwardedHeadersOptions>(options =>
        {
            options.KnownProxies.Add(IPAddress.Parse("192.168.1.220"));
        });

        services.Configure<TwilioVerifySettings>(Configuration.GetSection("Twilio"));

        //JWT Generate Token

        var appSettingsSection = Configuration.GetSection("AppSettings");
        services.Configure<AppSettings>(appSettingsSection);

        var appSettings = appSettingsSection.Get<AppSettings>();
        var key = Encoding.ASCII.GetBytes(appSettings.Secret);
        services.AddAuthentication(x =>
        {
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(x =>
        {
            x.RequireHttpsMetadata = false;
            x.SaveToken = true;
            x.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(key),
                ValidateIssuer = false,
                ValidateAudience = false
            };
        });

        // configure DI for application services
        services.AddScoped<IUserService, UserService>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
            endpoints.MapHub<ChatHub>("/ChatHub");
        });
    }
}

问题是您有不同的架构名称,正如您在提供的图片中看到的那样。

您在 ef core 3 的模型构建器中指定了不同的模式名称。您可以使用 modelBuilder.ToTable("Table_Name", "Schema_Name"); 来完成,也可以使用数据注释来完成。

无论哪种方式,您都可以在 Microsoft 上阅读这篇文章 https://docs.microsoft.com/en-us/ef/core/modeling/entity-types?tabs=data-annotations#table-schema

或者您为所有模型指定默认架构,这是我建议您做的。

public class SchoolContext: DbContext 
{
    public SchoolDBContext(): base() 
    {
    }

    public DbSet<Student> Students { get; set; }
    public DbSet<Standard> Standards { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Configure default schema
        modelBuilder.HasDefaultSchema("Admin");
    }
}

更多信息请点击此处: https://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.aspx