【发布时间】:2018-03-09 23:28:21
【问题描述】:
我正在尝试先在实体框架代码中创建一个数据库,但我总是收到这样的错误:
InvalidOperationException:无法将表“设备”用于实体类型 'IpMac' 因为它被用于实体类型 'Device' 并且主键 {'DeviceIP'} 和主键 {'DeviceID'} 之间没有关系。”
这些是我的模型类:
public class IpMac
{
[Key]
public string DeviceIP { get; set; }
//[ForeignKey("Device")]
public int DeviceID { get; set; }
public string DeviceMAC { get; set; }
//public Device Device { get; set; }
public ICollection<Device> Device { get; set; }
}
public class Device
{
//[Key]
public int DeviceID { get; set; }
public string DeviceName { get; set; }
public string UserName { get; set; }
//public string DeviceMAC { get; set; }
public IpMac IpMac { get; set; }
}
这是我的“DbContext”类:
public class DeviceContext : DbContext
{
public DeviceContext(DbContextOptions<DeviceContext> options) : base (options)
{
}
public DbSet<Device> Devices { get; set; }
public DbSet<IpMac> IpMacs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IpMac>().ToTable("Device");
modelBuilder.Entity<Device>().ToTable("Device");
Logger.Log("DeviceContext: Database & Tables SET.");
}
}
【问题讨论】:
标签: c# asp.net-mvc visual-studio entity-framework ef-code-first