Once you build the join table as an entity, you really lose the direct many-to-many relationship between Consultant and Program . Here's how it works. You will have either a many-to-many direct relationship or additional properties in the connection table. Not both. If you want, you can try creating your own Programs property on Consultant and use the linq query to get related programs:
public IEnumerable<Program> Programs { get { return this.ConsultantProgramLinks.Select(l => l.Program); } }
The example also explains your last problem. You cannot have the Program property on ConsultantProgramLink , because it is a collection of related objects, not a separate entity (it should be called ConsultantProgramLinks ). A property in a ConsultantProgramLink object is simply called a Program because it is a single entity, not a collection.
Edit:
If you need each Program be automatically associated with each Consultant , you must force it when you plan to create a new Program . Having a connection table open as a separate entity is likely to allow you to easily reach it:
var program = new Program(); ... context.Programs.AddObject(program); var ids = from c in context.Consultants select c.Id; foreach (var id in ids) { var link = new ConsultantProgramLink { ConsultantId = id, Program = program }; context.ConsultantProgramLinks.AddObject(link); } context.SaveChanges();
If you add a new Consultant , you will have to create links to all programs the same way.
The disadvantage is that if you have, for example, 1000 consultants, this design will create 1001 database inserts, where each insert will be performed in a separate reverse direction to the database. To avoid this, the only option is to either use a stored procedure or trigger in the program table.
Ladislav Mrnka
source share