EF Core Add-Migration 错误属性已存在
Posted
技术标签:
【中文标题】EF Core Add-Migration 错误属性已存在【英文标题】:EF Core Add-Migration error property already exists 【发布时间】:2021-12-28 10:50:53 【问题描述】:我正在尝试创建另一个迁移,但我不明白为什么会一直出现此错误。为什么它甚至尝试再次添加该属性?我在之前的迁移中添加了它,现在数据库是最新的,我想添加其他更改。我做错了什么?
PM> Add-Migration Northwind1.3
Build started...
Build succeeded.
System.InvalidOperationException: The property or navigation 'EmployeeRole' cannot be added to the entity type 'AdoNet.Entities.EF.Employee' because a property or navigation with the same name already exists on entity type 'AdoNet.Entities.EF.Employee'.
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddNavigation(MemberIdentity navigationMember, ForeignKey foreignKey, Boolean pointsToPrincipal)
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddNavigation(String name, ForeignKey foreignKey, Boolean pointsToPrincipal)
at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.Navigation(Nullable`1 propertyIdentity, ConfigurationSource configurationSource, Boolean pointsToPrincipal)
at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.SetDependentToPrincipal(String name, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalForeignKeyBuilder.HasNavigations(Nullable`1 navigationToPrincipal, Nullable`1 navigationToDependent, EntityType principalEntityType, EntityType dependentEntityType, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalForeignKeyBuilder.HasNavigations(Nullable`1 navigationToPrincipal, Nullable`1 navigationToDependent, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalForeignKeyBuilder.HasNavigation(String name, Boolean pointsToPrincipal, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.HasRelationship(EntityType targetEntityType, Nullable`1 navigationToTarget, Nullable`1 inverseNavigation, Nullable`1 setTargetAsPrincipal, ConfigurationSource configurationSource, Nullable`1 required)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.HasRelationship(EntityType targetEntityType, String navigationName, ConfigurationSource configurationSource, Nullable`1 targetIsPrincipal)
at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder.HasOneBuilder(MemberIdentity navigationId, EntityType relatedEntityType)
at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder.HasOne(String relatedTypeName, String navigationName)
at AdoNet.Migrations.NorthwindContextModelSnapshot.<>c.<BuildModel>b__0_31(EntityTypeBuilder b) in D:\Programming\Csharp\epam-inner\homework\adonet\AdoNet\Migrations\NorthwindContextModelSnapshot.cs:line 1086
at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(String name, Action`1 buildAction)
at AdoNet.Migrations.NorthwindContextModelSnapshot.BuildModel(ModelBuilder modelBuilder) in D:\Programming\Csharp\epam-inner\homework\adonet\AdoNet\Migrations\NorthwindContextModelSnapshot.cs:line 1084
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSnapshot.CreateModel()
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSnapshot.get_Model()
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The property or navigation 'EmployeeRole' cannot be added to the entity type 'AdoNet.Entities.EF.Employee' because a property or navigation with the same name already exists on entity type 'AdoNet.Entities.EF.Employee'.
员工:
public partial class Employee
public Employee()
EmployeeTerritories = new HashSet<EmployeeTerritory>();
InverseReportsToNavigation = new HashSet<Employee>();
Orders = new HashSet<Order>();
public int EmployeeId get; set;
public string LastName get; set;
public string FirstName get; set;
public string Title get; set;
public string TitleOfCourtesy get; set;
public DateTime? BirthDate get; set;
public DateTime? HireDate get; set;
public string Address get; set;
public string City get; set;
public string Region get; set;
public string PostalCode get; set;
public string Country get; set;
public string HomePhone get; set;
public string Extension get; set;
public byte[] Photo get; set;
public string Notes get; set;
public int? ReportsTo get; set;
public string PhotoPath get; set;
public int? EmployeeRoleId get; set;
public virtual EmployeeRole EmployeeRole get; set;
public virtual Employee ReportsToNavigation get; set;
public virtual ICollection<EmployeeTerritory> EmployeeTerritories get; set;
public virtual ICollection<Employee> InverseReportsToNavigation get; set;
public virtual ICollection<Order> Orders get; set;
员工角色:
public partial class EmployeeRole
public EmployeeRole()
Employees = new HashSet<Employee>();
public int EmployeeRoleId get; set;
public string EmployeeRoleName get; set;
public virtual ICollection<Employee> Employees get; set;
来自NorthwindContext中Employee的modelBuilder:
entity.HasOne(d => d.EmployeeRole)
.WithMany(p => p.Employees)
.HasForeignKey(d => d.EmployeeRoleId);
由于某种原因,EmployeeRole 中的 EmployeeRoleId 属性也被淡化了。
【问题讨论】:
【参考方案1】:我认为数据库中的 _MigrationHistory 表没有更新。检查 _MigrationHistory 以了解此表中应用的最后一次迁移
【讨论】:
我之前有 2 次迁移,它们都在 __EFMigrationsHistory 表中。 先检查数据库表的关系,确认无误。然后,如果不行,将导航或属性的名称( EmployeeRole virtual public EmployeeRole get; set; 更改为 EmployeeRole virtual public EmployeeRole _test get; set;; 数据库中的一切都是正确的 这应该是评论,而不是答案。如果您缺少评论代表(50),请避免发布应该是 cmets 的答案,因为您获得的任何反对票都会积极减损获得评论所需的代表以上是关于EF Core Add-Migration 错误属性已存在的主要内容,如果未能解决你的问题,请参考以下文章