kud*_*ger 5 c# sql-server migration entity-framework
我的此类仅包含外键引用:
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; }
}
Run Code Online (Sandbox Code Playgroud)
运行命令时出现错误
Add-Migration -Name "DeviceMigration"
Run Code Online (Sandbox Code Playgroud)
错误是:
无法将属性或导航“DeviceType”添加到实体类型“Device”,因为实体类型“Device”上已存在同名的属性或导航。
这是我的上下文类内容
public class MyContext: DbContext
{
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{ }
public DbSet<DeviceType> DeviceTypes { get; set; }
public DbSet<Device> Devices { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
按如下方式编写Device
模型类:
public class Device
{
[Required]
[DataMember(Name = "key")]
[Key]
public Guid Key { get; set; }
[ForeignKey("DeviceType")]
public Guid DeviceTypeId { get; set; } // I assumed primary key of your `DeviceType` entity is `Guid`
[ForeignKey("ModelType")]
public Guid ModelTypeId { get; set; } // I assumed primary key of your `ModelType` entity is `Guid`
[IgnoreDataMember]
public virtual DeviceType DeviceType { get; set; }
[IgnoreDataMember]
public virtual ModelType ModelType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在生成迁移。希望一切顺利。