i'm trying to make a database in entity-framework code-first but im always getting a error like this:
InvalidOperationException: Cannot use table 'Device' for entity type 'IpMac' since it is being used for entity type 'Device' and there is no relationship between the primary key {'DeviceIP'} and the primary key {'DeviceID'}."
these are my model classes:
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; }
}
and here is my "DbContext" class:
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.");
}
}
Shouldnt your device context use different table names for the tables?
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.");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With