I have this class with only the foreign key references:
public class Device
{
[Required]
[DataMember(Name = "key")]
[Key]
public Guid Key { get; set; }
[ForeignKey("DeviceType")]
[IgnoreDataMember]
public virtual DeviceType DeviceType { get; set; }
[ForeignKey("Model")]
[IgnoreDataMember]
public virtual ModelType Model { get; set; }
}
I get an error while running the command
Add-Migration -Name "DeviceMigration"
Error is:
The property or navigation 'DeviceType' cannot be added to the entity type 'Device' because a property or navigation with the same name already exists on entity type 'Device'.
This is my context class content
public class MyContext: DbContext
{
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{ }
public DbSet<DeviceType> DeviceTypes { get; set; }
public DbSet<Device> Devices { get; set; }
}
4条答案
按热度按时间ee7vknir1#
For my situation, I misused ForeignKey attribute:
whereas it should have been:
uttx8gqw2#
Write your
Device
model class as follows:Now generate the migration. Hope everything will work fine.
whlutmcx3#
If all solutions provided above fails, just delete the migrations folder and recreate the migrations. This error may arise due to visual studio cache references.
One could alternatively rename the property on the target entity.
brjng4g34#
I had a similar problem, which was caused by wrongly specifying the primary keys of the table.