Alex Watts Alex Watts - 1 month ago 10
C# Question

foreign key assignments using code first

I am creating a MVC application and want to generate the database using the Code First approach. I've done this with simple tables, but now that I am introducing Foreign Key relationships, things are getting muddled for me.

I have three classes, each of which will have their own table in the DataBase:

public class Device
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int InternalDeviceID { get; set; }

public int DeviceID { get; set; }

public string DeviceName { get; set; }

public virtual EquipmentDevice EquipmentDevice { get; set; }
}

public class Equipment
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int InternalEquipmentID { get; set; }

public int EquipmentID { get; set; }

public string EquipmentName { get; set; }

public virtual EquipmentDevice EquipmentDevice { get; set; }
}

public class EquipmentDevice
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int InternalEquipmentDeviceID { get; set; }

public virtual ICollection<Equipment> EquipmentID { get; set; }

public virtual ICollection<Device> DeviceID { get; set; }
}


The relationship is that a Equipment may have many Devices, and a Device may belong to many Equipments (a many to many if I'm not mistaken). To organize which Equipments have what Devices associated with them, I am using the EquipmentDevice table, which will just pair their IDs together along with an internal ID for that table.

Based on other examples I have found, I had believed this to be the way in which to code such a situation, where the Many side of a relationship will have an ICollection<> object of the Single side, and the Single side of a relationship will have a virtual object of the Many side (both classes having a ICollection<> of the other for a N:N, or both having a virtual for 1:1)

However, when I create the database, my tables look like this:

Device Tableenter image description here

Equipment Table
enter image description here

EquipmentDevice Table

enter image description here

Am I simply backwards in the way in which I am setting up foreign keys for my tables, or is there another problem I am missing altogether for this type of situation? I couldn't find an exceptionally helpful source of info on this subject. I'm also attaching what the diagram of these tables would look like as well, just to help be more clear.

enter image description here

Answer

You're close- the properties on Equipment and Device need to be ICollections. You don't need to define a class for the join table, EF will take care of that for you. Here's what your classes should look like (here's a good reference:)

http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

    public class Device
            { 
                public Device() 
                { this.Equipments = new HashSet<Equipment>();}

                [Key]
                [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
                public int InternalDeviceID { get; set; }

                public int DeviceID { get; set; }

                public string DeviceName { get; set; }

                public virtual ICollection<Equipment> Equipments { get; set; }
            }

    public class Equipment
        {

            public Equipment()
            { this.Devices = new HashSet<Device>(); }

            [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int InternalEquipmentID { get; set; }

            public int EquipmentID { get; set; }

            public string EquipmentName { get; set; }

            public virtual ICollection<Devices> Devices { get; set; }
        }