1

I will appreciate if somebody can tell me why entity framework is not creating join table for following model. It is creating table for type and feature but not the table that will join them.

public class DeviceType
    {
        [Display(Name = "ID")]
        public int DeviceTypeID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public IEnumerable<DeviceFeature> DeviceFeatures { get; set; }
    }

    public class DeviceFeature
    {
        [Display(Name = "ID")]
        public int DeviceFeatureID { get; set; }

        [Required]        
        public string Name { get; set; }
        public string Description { get; set; }

        public IEnumerable<DeviceType> DeviceTypes { get; set; }

    }

    public class DeviceFeatureView
    {
        public virtual IEnumerable<DeviceType> DeviceTypes { get; set; }
        public virtual IEnumerable<DeviceFeature> DeviceFeatures { get; set; 
    }
1
  • Change IEnumerable<DeviceFeature> to ICollection<DeviceFeature> in both entity classes. ICollection<T> is the minimum requirement for EF collection navigation property. Commented Apr 22, 2017 at 17:31

1 Answer 1

1

You do not need the bridge to create a many-to-many relationship. EF will figure it out. Change the type of the navigation properties from IEnumerable to ICollection like this:

public class DeviceType
{
    public DeviceType() 
    {
        this.DeviceFeatures = new HashSet<DeviceFeature>();
    }
    [Display(Name = "ID")]
    public int DeviceTypeID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public ICollection<DeviceFeature> DeviceFeatures { get; set; }
}

public class DeviceFeature
{
    public DeviceFeature() 
    {
        this.DeviceTypes = new HashSet<DeviceType>();
    }   
    [Display(Name = "ID")]
    public int DeviceFeatureID { get; set; }

    [Required]        
    public string Name { get; set; }
    public string Description { get; set; }

    public ICollection<DeviceType> DeviceTypes { get; set; }

}

More about it here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you CodingYoshi, I could have never figured it out that quick. It is working now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.