I have a simple C# class that is a model of a table I'd like to have in SQL Server. Here's the class:
public class Device
{
public int Id { get; set; }
public DeviceType DeviceType { get; set; }
public IList<Device> ConnectedDevices { get; set; }
}
When I create a migration, here's what Entity Framework generates for this table:
CreateTable(
"dbo.Devices",
c => new
{
Id = c.Int(nullable: false, identity: true),
Device_Id = c.Int(),
DeviceType_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Devices", t => t.Device_Id)
.ForeignKey("dbo.DeviceTypes", t => t.DeviceType_Id)
.Index(t => t.Device_Id)
.Index(t => t.DeviceType_Id);
I see that it creates Id and Device_Type_Id - these two are fine. But why does it want to create Device_Id, which is Int? I don't need that - I want to have a one-to-many relationship (one device can be a master of many devices). So why doesn't it reflect my ConnectedDevices property properly?
//EDIT: When I think about it more, actually I should get many-to-many relation. Every device can have many devices that it controls. So EF should generate for me additional table to satisfy that. I can't understand why it doesn't do that