nhibernate one-to-one mapping and not-null="false"?
How to create one-to-one relation using NHibernate where other side could
be a NULL? For example, I have a Banner entity that has a relation to
Image entity like this:
<class name="Banner" table="Banner">
<id name="Id" type="Int64" unsaved-value="0">
<generator class="native" />
</id>
<many-to-one name="Image" unique="true" column="ImageId"
not-null="false"/>
<!-- check: In the example this property was without inverse attribute
-->
<set name ="BannerSlideSet" fetch="subselect" inverse="true">
<key column="Banner_ID" foreign-key="FK_Banner_BannerSlide"
not-null="false" />
<one-to-many class="BannerSlide"/>
</set>
</class>
So, Image can be null for a Banner entity. I create one banner entity with
no image without any issues. In DB I have
--------------
ID | ImageId
--------------
1 | NULL
After that, I'm trying to create second Banner instance with no Image as
well and get the following error:
NHibernate.Exceptions.GenericADOException: could not insert:
[Domain.Entities.Banner][SQL: INSERT INTO Banner (ImageId) VALUES (?);
select SCOPE_IDENTITY()] ---> System.Data.SqlClient.SqlException:
Violation of UNIQUE KEY constraint 'UQ_Banner_7516F70DE6141471'. Cannot
insert duplicate key in object 'dbo.Banner'. The duplicate key value is
().
I guess, it happens because I have unique constraint on one-to-many
relation between banner and image entities and several Banner instances
can't have several NULL values in ImageId field. Question: how I would
achieve one-to-nullableone relation with in NHinerbate?
Thanks!
No comments:
Post a Comment