Click here to Skip to main content
14,983,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 2 classes `ConfigurationCollection` and `OptionValues` which has a many to many relationship. I have tried solutions on the net to get it working. I tried adding another class to create two one to many relationship. But I am not understanding it the right way I suppose. And I also tried it without adding a third class.

Model classes

C#
public class ConfigurationCollection
{
    public int ConfigurationCollectionID { get; set; }
    public string CollectionName { get; set; }
    public int LsystemID { get; set; }

    public virtual Lsystem Lsystem { get; set; }
    public virtual ICollection<OptionValue> OptionValues { get; set; }
}
public class OptionValue
{
    public int OptionValueID { get; set; }
    public string OptionVal { get; set; }
    public int OptionID { get; set; }

    public virtual ICollection<ConfigurationCollection> ConfigurationCollections { get; set; }
    public virtual Option Option { get; set; }
}
public class Config_OptionVal
{
    public int Config_OptionValID { get; set; }
    public int OptionValueID { get; set; }
    public int ConfigurationCollectionID { get; set; }
    public bool OptionValChecked { get; set; }

    public virtual OptionValue OptionValue { get; set; }
    public virtual ConfigurationCollection ConfigurationCollection { get; set; }
}


Controller

C#
public ActionResult Create(int LsystemID)
{
    var model = new ConfigurationCollection
    {
      LsystemID = LsystemID,
      Lsystem = db.Lsystem.FirstOrDefault(x => x.LsystemID == LsystemID),
      OptionValues=new List<OptionValue>()
    };
    return View(model);
}


I am not sure what should be written in the Controller. I am able to populate the `OptionValues` in my View. I am just missing something that could save the data into the database.
Posted

1 solution

In order to create a M->M relationship in entity Framework you'll need to use a mapping class, as you did with Config_OptionVal. Now it's important that that mapping class is the one that the classes on either side of the relationship operate through, you can't ignore that entity in the relationship. Therefore:

C#
public class ConfigurationCollection
{
    public int ConfigurationCollectionID { get; set; }
    public string CollectionName { get; set; }
    public int LsystemID { get; set; }
 
    public virtual Lsystem Lsystem { get; set; }
    public virtual ICollection<Config_OptionVal> Config_OptionVals{ get; set; }
}
public class OptionValue
{
    public int OptionValueID { get; set; }
    public string OptionVal { get; set; }
    public int OptionID { get; set; }
 
    public virtual ICollection<Config_OptionVal> Config_OptionVals{ get; set; }
    public virtual Option Option { get; set; }
}
public class Config_OptionVal
{
    public int Config_OptionValID { get; set; }
    public int OptionValueID { get; set; }
    public int ConfigurationCollectionID { get; set; }
    public bool OptionValChecked { get; set; }
 
    public virtual OptionValue OptionValue { get; set; }
    public virtual ConfigurationCollection ConfigurationCollection { get; set; }
}


Access across this relationship uses Config_OptionVal, like this:

C#
myContext.Set<config_optionval>().Where(x => x.OptionValueID == IDofInterest).ToArray();

myContext.Set<Config_OptionVal>().Where(x => x.ConfigurationCollectionID == IDofInterest).ToArray();

ConfigurationCollectionObject.Config_OptionVal.Select(x => x. OptionValue).ToArray();

OptionValueObject.Config_OptionVal.Select(x => x.ConfigurationCollection).ToArray();
</config_optionval>


So in your controller example, assuming LSystem and ConfigurationCollection are 1->1:

C#
public ActionResult Create(int LsystemID)
{
    var model = new ConfigurationCollection
    {
      LsystemID = LsystemID,
      Lsystem = db.Lsystem.FirstOrDefault(x => x.LsystemID == LsystemID),
      OptionValues= Lsystem != null 
          ? Lsystem.ConfigurationCollection.Config_OptionVal.Select(x => x.OptionValues).ToList() 
          : new List<optionvalue>();
    };
    return View(model);
}
</optionvalue>


And a couple of examples/tutorials:
MVC 5, Entity Framework 6 and Many to Many Relationship : a step by step View Model approach[^]

https://msdn.microsoft.com/en-us/library/dd742359.aspx[^]
   
Comments
vini vasundharan 5-Nov-15 8:12am
   
How do i post the values from my View to the Create post action method? I display all the Option values in my View and the user selects the required Option Values. How do I access all these values in the post and save it into the database. I suppose these values should be saved in the Config_OptionVal table
Nathan Minier 5-Nov-15 8:17am
   
You should only be modifying the OptionValue table when you actually add new option values that can be selected. Tracking selected values should be done on Config_OptionVal.

Honestly, taking another look at this, you've got an extra layer of abstraction that you do not need. Let me modify the answer to reflect a more sensible approach.

Scratch that, it looks like you want multiple configuration sets per LSystem, so disregard last sentence please.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900