Many-To-Many workaround

Nov 15 at 11:42 AM
Hi All,

I am new to ORMs in general and I am encountering issues with my mapping.
Does someone have a tip to map many-to-many relations ? I know it is not supported, but I guess someone should have figured a solution out by now ?

This is what my tables look like in the database:
  • Molecules (Name [PK], MolarMass)
  • Cylinders (Name [PK])
  • CylindersComposition (Cylinder [Ref Cylinder.Name], Molecule [Ref Molecule.Name], Concentration)
This is how I tried to map the objects:
[Entity(NameInStore="Molecules", KeyScheme=KeyScheme.Identity)]
    public class Molecule
    {
        /// <summary>
        /// Name of the molecule
        /// </summary>
        [Field(IsPrimaryKey = true, RequireUniqueValue = true, AllowsNulls=false)]
        public string Name { get; set; }

        /// <summary>
        /// Molecular mass of the molecule (g/mol)
        /// </summary>
        [Field(FieldName="MolarMass", AllowsNulls=false)]
        public float MolecularMass { get; set; }
}

[Entity(NameInStore="Cylinders", KeyScheme=KeyScheme.Identity)]
    public class Cylinder
    {
        /// <summary>
        /// Name of the cylinder
        /// </summary>
        [Field(IsPrimaryKey=true,AllowsNulls=false,RequireUniqueValue=true)]
        public string Name { get; set; }

        /// <summary>
        /// List of gases present in the cylinder
        /// </summary>
        [Reference(typeof(CylinderComposition), "Cylinder", LocalReferenceField="Name",Autofill=true, ReferenceType = ReferenceType.ManyToOne)]
        public CylinderComposition[] Components {get;set;}
    }

// This one is the copy of my last desperate attempt
[Entity(NameInStore="CylindersComposition",KeyScheme=KeyScheme.None)]
    public class CylinderComposition
    {
        [Field(AllowsNulls = false)]
        public string Cylinder { get; set; }

        [Reference(typeof(CylinderComposition), "Cylinder", LocalReferenceField = "Cylinder", Autofill = true, ReferenceType = ReferenceType.ManyToOne)]
        public Cylinder CylinderObject { get; set; }

        [Field(AllowsNulls = false)]
        public string Molecule { get; set; }

        [Reference(typeof(CylinderComposition), "Molecule", LocalReferenceField = "Molecule", Autofill = true, ReferenceType = ReferenceType.ManyToOne)]
        public string MoleculeObject { get; set; }

        [Field(AllowsNulls= false)]
        public float Concentration { get; set; }
    }
It does not work as is.

I would appreciate if someone could give me a hand with this.

Thanks !