J'ai 2 tables dans la base de données. Formule et ingrédient. Ils ont une relation many-to-many, donc j'ai une table d'association dans la base de données appelée FormulaIngredient. J'utilise C# .net et SQL Server 2005. Ma table FormulaIngredient a ID, formulaID, ingredientID, ingredientAmount. Pour ce champ supplémentaire de l'ingrédientAmount, j'ai créé une classe d'association en C#. Maintenant, je vais enregistrer une formule dans la base de données. et après cela, je veux sauvegarder la liste des ingrédients de cette formule dans le tableau FOrmulaIngredient. Comment puis-je le faire? Je ne peux pas enregistrer de données dans la table FormulaIngredient. Ma classe FormulaIngredient estComment insérer des données dans une table Association many-to-many en utilisant NHibernate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NutritionLibrary.Entity
{
public class FormulaIngredient
{
private int iD;
private Formula objFormula;
private Ingredient objIngredient;
private float ingredientAmount;
public FormulaIngredient()
{
}
public virtual int ID
{
get { return iD; }
set { iD = value; }
}
public virtual int IngredientID
{
get { return objIngredient.IngredientID; }
set { objIngredient.IngredientID = value; }
}
public virtual int FormulaID
{
get { return objFormula.FormulaID; }
set { objFormula.FormulaID = value; }
}
public virtual Ingredient ObjIngredient
{
get { return objIngredient; }
set { objIngredient = value; }
}
public virtual Formula ObjFormula
{
get { return objFormula; }
set { objFormula = value; }
}
public virtual float IngredientAmount
{
get { return ingredientAmount; }
set { ingredientAmount = value; }
}
}
}
est le fichier de mappage ici pour FormulaIngredient:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NutritionLibrary.Entity.FormulaIngredient, NutritionLibrary" table="NutrientIngredient" lazy="false">
<id name="ID" column="ID" >
<generator class="native" />
</id>
<many-to-one name="ObjIngredient" column="ingredientID" class="NutritionLibrary.Entity.Ingredient, NutritionLibrary" not-null="true" />
<many-to-one name="ObjFormula" column="formulaID" class="NutritionLibrary.Entity.Formula, NutritionLibrary" not-null="true" />
<property name="IngredientAmount" column="ingredientAmount" type="float" not-null="true" />
</class>
</hibernate-mapping>
S'il vous plaît aider !!!!
Assurez-vous de mettre en retrait votre code de 4 espaces lorsque vous postez afin qu'il soit correctement ramassé en tant que code. –