Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need to map your join table with its own type. And also, you shouldn't expose Formula.ID from a property of another class. Here is what you need</p> <p>3 database Tables</p> <pre><code>Ingredients: PK:ID(Identity(1,1), Name, Description Formula: PK:ID(Identity(1,1), Name, Description IngredientFormulaJoin: FK:IngredientID, FK:FormulaID create table dbo.Ingredients(id int primary key identity(1,1), Name varchar(100)) create table dbo.formulas(id int primary key identity(1,1), Name varchar(100)) create table dbo.ingredientformulajoin(ingredientid int foreign key references dbo.ingredients(id), formulaid int foreign key refernces dbo.formula(id)) public class Forumula() { public Formula(){ Ingredients = new List&lt;Ingredient&gt;(); } public int PK:ID(Identity(1,1) { get; set; } public IList&lt;Ingredient&gt; Ingredients{ get; set; } } public class Ingredient() { public Ingredient(){ Formulas = new List&lt;Formula&gt; } public int ID { get; set; } public IList&lt;Forumula&gt; Formulas { get; set; } } </code></pre> <p>Here is the mapping:</p> <pre><code>&lt;class name="App.Core.Domain.Ingredient, App.Core" table="ingredients"&gt; &lt;set name="Formulas" table="formulaingredientjoin" inverse="false" cascade="all"&gt; &lt;key column="ingredientid"/&gt; &lt;many-to-many class="App.Core.Domain.Forumula, App.Core" column="formulaid"/&gt; &lt;/set&gt; &lt;/class&gt; &lt;class name="App.Core.Domain.Formula, App.Core" table="formulas"&gt; &lt;set name="Ingredients" table="formulaingredientjoin" inverse="false" cascade="all"&gt; &lt;key column="formulaid"/&gt; &lt;many-to-many class="App.Core.Domain.Ingredient, App.Core" column="ingredientid"/&gt; &lt;/set&gt; &lt;/class&gt; </code></pre> <p>Add to the collections like this:</p> <pre><code>Formula formula = new Formula(); formula.Ingredients.Add(ingredient1) formula.Ingredients.Add(ingredient2) formula.Ingredients.Add(ingredient3) session.Save(formula); session.Flush(); Ingredient ingredient = new Ingredient(); ingredeient.Formulas.Add(formula1); ingredeient.Formulas.Add(formula2); session.Save(ingredient); session.Flush(); </code></pre> <p>You should never need to Map a join table as its own class. Also allow classes to encapsulate their own types. The only thing that should return a formula ID is a formula. You wouldn't put 'FormulaID' in ingredients for example, rather, call formula.ID.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload