I\'m doing a fairly complex NHibernate transaction in a financial system, creating a payment, recording the ledger entries, checking to see if the payment is the total amoun
In my case it was a missing Identity Specification on the SQL-Server.
Simple object:
public class Employee
{
public virtual int ID { get; set; }
}
Mapping:
public class EmployeeMap : ClassMapping<Employee>
{
public EmployeeMap()
{
Id(x => x.ID, map => { map.Generator(Generators.Identity); map.UnsavedValue(0); });
}
}
SQL:
Here is the ID column with the primary key constraint.
And here you can see the missing Identity Specification, which is causing the problem.
To solve the problem, you have to specify the ID column as IDENTITY
i.e.
CREATE TABLE EMPLOYEE
(
ID int NOT NULL IDENTITY(0, 1)
);
You should check CFAPTransaction mapping, It looks like you wanted to specify one Vendor for each transaction. In this case your mapping has to be like below code.
public CFAPTransactionMap()
{
HasOne(x => x.Vendor).ForeignKey("VendorId").Cascade.All();
...
}
It's likely that nhibernate is not showing the correct property of error, check the adjacent properties in the mapping file, looking for errors in relationship between data types from your database and data types from .net or repeated columns in properties... also check this link Fluent NHibernate - IndexOutOfRange
In my case, the exception was accurately identifying the property that was the cause of the error. I had a many-to-one property that was lacking a cascade
definition. "save-update"
prevents the error:
<many-to-one name="FocusType" cascade="save-update"
class="MyInfrastructure.FocusType, MyInfrastructure">
<column name="FocusTypeId" sql-type="int" not-null="false" />
</many-to-one>
a smart person once told me to add this to my mapping
.Length(int.MaxValue);
I encountered the same error. This is my sample mappings:
ManyToOne(x => x.objPerson, map => { map.Column("PersonID"); map.NotNullable(false); });
Property(x => x.intPersonID, map => map.Column("PersonID"));
If I tried to persist/save this on my database by populating only the property intPersonID
and making the objPerson
null, this will trigger the dehydrating error on all your properties!
The reason I am just populating intPersonID
is to prevent querying on the database to get the objPerson
before saving to the database. Unfortunately, it will trigger an error, so I modified my mappings and corrected with this:
ManyToOne(x => x.objPerson, map => { map.Column("PersonID"); map.NotNullable(false); });
Or if I want to prevent querying on the database by getting the whole object, I will just use this mapping instead:
Property(x => x.intPersonID, map => map.Column("PersonID"));
But combining them is not possible.