I have read thousands of threads but I still don't understand! :(
I'll just getting:
Cannot insert the value NULL into column 'SupplierId' in table Brands.
What am I doing wrong?
Code:
string AssamblyName = System.Configuration.ConfigurationManager.AppSettings["DefaultEntities"].ToString();
Configuration cfg = new Configuration();
cfg.AddAssembly(AssamblyName);
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
Supplier s = new Supplier();
s.Name = "foo";
s.CreateDate = DateTime.Now;
s.LastUpdatedDate = s.CreateDate;
s.Brands = new List<Brand>();
Brand b = new Brand("bar");
s.Brands.Add(b);
session.Save(s);
transaction.Commit();
session.Close();
session.Dispose();
Response.Write("oki");
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Swh.Entities.Supplier, Swh.Entities" table="Suppliers">
<id name="Id" column="SupplierId" type="int" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Name" column="SupplierName" type="String"/>
<property name="CreateDate" column="CreateDate" type="DateTime"/>
<property name="LastUpdatedDate" column="LastUpdatedDate" type="DateTime"/>
<bag name="Brands" lazy="true" cascade="all-delete-orphan" inverse="true">
<key>
<column name="SupplierId" not-null="true"/>
</key>
<one-to-many class="Swh.Entities.Brand, Swh.Entities"/>
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Swh.Entities.Brand, Swh.Entities" table="Brands">
<id name="Id" column="BrandId" type="int" unsaved-value="0">
<generator class="identity" />
</id>
<many-to-one name="Supplier" class="Swh.Entities.Supplier, Swh.Entities" column="SupplierId" />
<property name="Name" column="BrandName" type="String"/>
</class>
</hibernate-mapping>