Search code examples
entity-frameworkcodefluent

ManyToMany association table with properties


I need to know if it is possible to add properties on a many to many Relationship or wether I should go for adding an entity with Relationship ? In the case of the extra entity, how can I get the two referenced tables to participate in the key (to get a compound key of the two tables) ?

I followed this article but it doesn't go deep enough :http://blog.codefluententities.com/2012/06/14/many-to-many-relationships-with-codefluent-entities/

Thanks in advance,


Solution

  • You have to create an entity with relationship. This new entity has a composite key, so you also have to set setType="List"

    <cf:entity name="Student">
      <cf:property name="Id" key="true" />
      <cf:property name="Name" />
      <cf:property name="Enrollments" typeName="{0}.EnrollmentCollection" relationPropertyName="Student" />
    </cf:entity>
    
    <cf:entity name="Course">
      <cf:property name="Id" key="true" />
      <cf:property name="Name" />
      <cf:property name="Enrollments" typeName="{0}.EnrollmentCollection" relationPropertyName="Course" />
    </cf:entity>
    
    <cf:entity name="Enrollment" setType="List">
      <cf:property name="Course" key="true" typeName="{0}.Course" relationPropertyName="Enrollments" />
      <cf:property name="Student" key="true" typeName="{0}.Student" relationPropertyName="Enrollments" />
      <cf:property name="Prop1" />
      <cf:property name="Prop2" />
    </cf:entity>