简体   繁体   中英

EF Core Add-Migration error property already exists

I'm trying to create another migration, and I don't understand why I keep having this error. Why does it even try to add the property again? I added it on the previous migration, now database is up to date, and I want to add other changes. What am I doing wrong?

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'.

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; }
}

EmployeeRole:

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; }
}

From the modelBuilder of Employee in NorthwindContext:

entity.HasOne(d => d.EmployeeRole)
    .WithMany(p => p.Employees)
    .HasForeignKey(d => d.EmployeeRoleId);

Also the EmployeeRoleId property in EmployeeRole is faded for some reason.

I think the _MigrationHistory tables in Database are not updated. Check the _MigrationHistory for the last migration applied in this table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM