You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC中如何让Identity使用现有Unidades数据库表?

Fix EF Core Trying to Create New Unidades Table Instead of Using Existing One

It sounds like EF Core isn’t recognizing your existing Unidades table, or the relationship between ApplicationUser and Unidades isn’t configured properly. Here’s how to resolve this step by step:

1. Verify the Unidades Entity Class

First, make sure your Unidades entity matches your existing table’s schema exactly. This includes:

  • Property names that align with column names (use the [Column] attribute if names differ)
  • Correct data types (e.g., int for integer columns, string for varchar fields)
  • A defined primary key (use [Key] attribute or Fluent API)

Example of a properly aligned entity:

public class Unidades
{
    [Key]
    public int Id { get; set; } // Match your existing primary key column name/type
    public string Nombre { get; set; } // Match other columns from your table
    // Add additional properties that correspond to your existing table’s columns
}

2. Register Unidades in Your DbContext

Ensure your DbContext includes a DbSet<Unidades> to reference the existing table. If it’s missing, add it:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Unidades> Unidades { get; set; }

    // Constructor and other existing configurations
}

3. Explicitly Map the Entity to the Existing Table

Use Fluent API in your DbContext’s OnModelCreating method to tell EF Core exactly which table Unidades maps to, and configure the relationship with ApplicationUser:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder); // Don’t skip this—it preserves Identity configurations

    // Map Unidades to your existing table
    modelBuilder.Entity<Unidades>()
        .ToTable("Unidades"); // Explicitly specify the existing table name

    // Configure the one-to-one/one-to-many relationship with ApplicationUser
    modelBuilder.Entity<ApplicationUser>()
        .HasOne(u => u.Unidad)
        .WithMany() // Adjust to .WithMany(u => u.Users) if Unidades has a collection of ApplicationUsers
        .HasForeignKey("UnidadId"); // Specify the foreign key column in AspNetUsers (ensure this column exists or will be added via migration)
}

Note: If your AspNetUsers table doesn’t already have a foreign key column to Unidades, EF can generate a migration for that change—just not the Unidades table itself.

4. Prevent Migrations from Creating the Unidades Table

Since the table already exists, you need to stop EF from trying to recreate it:

For first-time setup:

  1. Run Add-Migration InitialCreate -IgnoreChanges in the Package Manager Console. This creates a baseline migration that ignores all existing tables.
  2. Run Update-Database to apply the empty migration (it won’t modify your existing tables).

If you have existing migrations:

  1. Open your latest migration file and delete any CreateTable code for the Unidades table.
  2. Run Update-Database to apply the migration without recreating the table.

5. Double-Check Your Connection String

Ensure your connection string in appsettings.json points to the correct database where the Unidades table lives—it’s easy to accidentally target a different database!

By following these steps, EF Core will recognize your existing Unidades table and use it for the ApplicationUser relationship instead of trying to create a new one.

内容的提问来源于stack exchange,提问作者Santiago Calgaro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:20:46