I have a database of three tables:
FaceT PhotoT FacePhotoT
------ ------ ----------
FaceID PhotoID FaceID
Name Name PhotoID
Some additional columns
Actually, its two one to many relationships because of additional columns in link table. Anyway, inserting completely new records is working fine, but the question is, how I suppose to do record insert if I just want to insert new face to existing photo record? I get fresh new face name and existing photo name in which that face exists and what to do? I cannot create another photo record, because there are such file with same name uploaded to server (Name column in Photo table is unique), I have to check somehow if photo doesn't exist in the database and if it does, create records only in two other tables (FaceT and FacePhotoT), but I don't know how.
Assuming the following entity and mapping for PhotoT
:
public class PhotoT
{
//your entity's other properties here
public virtual IList<FaceT> Faces { get; set; }
public PhotoT()
{
Faces = new List<FaceT>();
}
}
public class PhotoTMap : ClassMap<PhotoT>
{
public PhotoTMap()
{
//mapping for other properties here
HasManyToMany(x => x.Faces)
.Cascade.SaveUpdate() //or another Cascade option of your choice
.Inverse() //or Inverse on FaceT
.Table("FacePhotoT")
.ParentKeyColumn("PhotoID")
.ChildKeyColumn("FaceID");
}
}
You could just add a new FaceT
to the collection of an existing PhotoT
record like this:
PhotoT photo = session.Get<PhotoT>(1);
FaceT face = new FaceT();
photo.Faces.Add(face);
session.Update(photo);
tx.Commit();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.