Answer the question
In order to leave comments, you need to log in
How to insert new record into table with composite key using fluent nhibernate?
Good day to all.
I have a Person entity.
I have the entity Program.
Each Person can have multiple Programs and each Program can have multiple Persons.
There is an entity PersonProgram.
Using Fluent NHibernate I create classes like this:
// Person
public class Person : Entity
{
public virtual Int32 Id { get; set; }
public virtual IList<PersonProgram> Programs { get; set; }
...
}
public class PersonMap : ClassMap<Person>
{
Table("Person");
Id(p => p.Id).Column("Id");
HasMany(p => p.Programs).KeyColumn("personID").Inverse();
...
}
// Program
public class Program : Entity
{
public virtual Int32 Id { get; set; }
...
}
public class ProgramMap : ClassMap<Program>
{
Table("Program");
Id(p => p.Id).Column("Id");
...
}
// PersonProgram
public class PersonProgram : Entity
{
public virtual Person person { get; set; }
public virtual Program Program { get; set; }
...
}
public class PersonProgramMap : ClassMap<PersonProgram >
{
Table("PersonProgram");
CompositeId()
.KeyReference(p => p.Person, "personID")
.KeyReference(p => p.Program, "programID");
...
}
var newPersonProgram = new PersonProgram();
newPersonProgram.Person = existedPerson;
newPersonProgram.Program = existedProgram;
...
using (ITransaction transaction = _session.BeginTransaction())
{
_session.SaveOrUpdate(newPersonProgram); // -- ошибка, что countRow = 2, ожидается 1
//_session.Save(newPersonProgram); -- ошибка, что countRow = 2, ожидается 1
//_session.Merge(newPersonProgram); -- ошибка, что поле personID не может быть заполнено значением NULL
transaction.Commit();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question