【发布时间】: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 属性也被淡化了。
【问题讨论】:
标签: c# sql-server entity-framework-core entity-framework-migrations dbcontext