ASP.NET MVC从数据库更新模型出错:如何解决两类关联问题?
Let's work through your two issues step by step—both are directly related to how Entity Framework (EF) interprets your database schema, specifically the missing primary key on the DisplayAdapters table.
1. Removing Unwanted MachineInfo1 and MachineInfo2 Properties
Why This Happens
EF generates these extra navigation properties when it can't correctly infer relationships between tables. Since DisplayAdapters had no primary key defined, EF didn't recognize it as a standalone entity. Instead, it tried to guess the relationship incorrectly, leading to duplicate/extra navigation properties on your MachineInfo model.
Fix Steps
First, we'll fix the database schema (adding a primary key to DisplayAdapters), then update your EF model to clean up these properties:
Add a Primary Key to
DisplayAdapters
Run this SQL to add an auto-incrementing primary key column to the table:USE [sales] GO ALTER TABLE [dbo].[DisplayAdapters] ADD [Id] INT IDENTITY(1,1) NOT NULL CONSTRAINT [PK_DisplayAdapters] PRIMARY KEY CLUSTERED ([Id] ASC) GOThis gives EF a clear identifier for each
DisplayAdaptersrecord, eliminating ambiguous relationship guesses.Update Your EF Model
- If you're using Database First: Delete your existing
MachineInfoandDisplayAdaptersmodel entities, then re-generate them from the database. EF will now correctly map the relationship without creating extra properties. - If you're using Code First: Manually delete the
MachineInfo1andMachineInfo2properties from yourMachineInfoclass. Then use Fluent API to explicitly define the relationship (see section 2 below).
- If you're using Database First: Delete your existing
2. Configuring One-to-Many Relationship Between MachineInfo and DisplayAdapters
Why This Was a 0..1 Relationship
Without a primary key on DisplayAdapters, EF assumed each MachineInfo could have at most one DisplayAdapters record (0..1), because it couldn't distinguish multiple records for the same ProfileHash. Adding a primary key lets EF recognize that multiple DisplayAdapters can belong to a single MachineInfo.
Fix Steps
For Database First
After adding the primary key and re-generating the model, EF should automatically create a one-to-many relationship:
- Your
MachineInfomodel will have anICollection<DisplayAdapters>navigation property. - Your
DisplayAdaptersmodel will have a singleMachineInfonavigation property linked via theProfileHashforeign key.
For Code First
Use Fluent API in your DbContext to explicitly configure the relationship:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Define one-to-many: One MachineInfo has many DisplayAdapters modelBuilder.Entity<MachineInfo>() .HasMany(machine => machine.DisplayAdapters) .WithRequired(adapter => adapter.MachineInfo) .HasForeignKey(adapter => adapter.ProfileHash); }
Make sure your model classes match this:
MachineInfoincludespublic virtual ICollection<DisplayAdapters> DisplayAdapters { get; set; }DisplayAdaptersincludespublic virtual MachineInfo MachineInfo { get; set; }
Final Checks
- Verify that the foreign key constraint
FK_DisplayAdapters_MachineInfostill exists (it should, since we only added a primary key and didn't modify the foreign key). - Test updating/querying the model to ensure the relationship works as expected—you should now be able to retrieve multiple
DisplayAdaptersfor a singleMachineInfowithout extra properties cluttering your model.
内容的提问来源于stack exchange,提问作者abenci

