ASP.NET MVC中如何让Identity使用现有Unidades数据库表?
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.,
intfor integer columns,stringfor 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:
- Run
Add-Migration InitialCreate -IgnoreChangesin the Package Manager Console. This creates a baseline migration that ignores all existing tables. - Run
Update-Databaseto apply the empty migration (it won’t modify your existing tables).
If you have existing migrations:
- Open your latest migration file and delete any
CreateTablecode for theUnidadestable. - Run
Update-Databaseto 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

