Many to Many with additional field in middle table
Good day,
i would like to use FH in my project. Here is the question came up in the middle of the way:
Lets say i have two tables related as many to many:
ProcessTable:
Id
Name
Description
PluginTable:
Id
Name
Description
Here is the middle table created by FH:
ProcessToPlugin:
Process_Id
Plugin_Id
Question - i would need to have one more field in ProcessToPlugin table, so it would like:
ProcessToPlugin:
Process_Id
Plugin_Id
PluginSettings_Id
PluginSettings_Id is some digit value used program.
Which settings in FH i may do to have this structure? Is it possible?
Support Staff 2 Posted by Paul Batum on 28 Jun, 2010 11:22 AM
Once your joining table for a many-to-many has an additional field on it,
its not really a many to many anymore. The common way that people solve it
is like this:
public class Process
{
public IList<ProcessPlugin> Plugins { get; set; }
}
public class ProcessPlugin
{
public Process Process { get; set; }
public Plugin Plugin { get; set; }
public Settings Settings { get; set; }
}
public class Plugin
{
public IList<ProcessPlugin> Processes { get; set; }
}
public ProcessMap()
{
HasMany(x => x.Plugins);
}
public ProcessPluginMap()
{
References(x => x.Process);
References(x => x.Plugin);
References(x => x.Setting);
}
public PluginMap()
{
HasMany(x => x.Processes);
}
Obviously this works but its not perfect. You can also make those lists
private and then expose them as a list of Plugins (instead of a list of
ProcessPlugins).
The other alternative that might work in your case is a ternary mapping. It
uses a dictionary, something like this:
public class Process
{
public IDictionary<Settings, Plugin> Plugins { get; set; }
}
But this is a bit different and I'm not sure if it can work for your
scenario.