我正在尝试在实体框架代码中创建一个数据库,但我总是得到这样的错误:
InvalidOperationException:无法将表'Device'用于实体类型 'IpMac',因为它被用于实体类型'Device',主键{'DeviceIP'}和主键{'DeviceID'}之间没有任何关系。“
这些是我的模特课:
$query = "SELECT t1.Ia f1, t2.Ia f2, t1.Ia+t2.Ia added_values, t1.time,t2.time
from ".$tbl1." t1 INNER JOIN ".$tbl2." t2
ON CONVERT(char(10), t1.datetime,126) = CONVERT(char(10), t2.datetime,126)
where CONVERT(char(10), t1.datetime,126) = '".$date."';";
这是我的“DbContext”类:
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; }
}
答案 0 :(得分:3)
您的设备上下文不应该为表使用不同的表名吗?
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("IpMac");
modelBuilder.Entity<Device>().ToTable("Device");
Logger.Log("DeviceContext: Database & Tables SET.");
}
}