I have problems generating a new table by using HasManyToMany only When I have a IDictonary as a source, for instance If I try it with an IList I do not have any problem and I see how a new table is generated and the relations. But I need to use a IDictioonaryThe example:
Thank you very much for your patient and your answer.
I do not know If i have understud properly the concept of this: " Both sides of the now one to many need to be saved or updated first. "
For instance, I have created a 2 new tables to do the tests. Teachers and Classes. They have only the attitubutes Id and Name. And I have added a relation one to many from teacher to classes.
public class Classes : I_DTO
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public Classes()
{
}
}
namespace ExCon_DAO_SQL.DTO.Wall
{
public class Teacher : I_DTO
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Classes> Classes { get; set; }
public Teacher()
{
Classes = new List<Classes>();
}
}
public class Teacher_Map : IAutoMappingOverride<Teacher>
{
public void Override(AutoMapping<Teacher> mapping)
{
mapping.HasMany(x => x.Classes).LazyLoad().
Inverse().AsBag().ForeignKeyConstraintName("Teacher_Id").Cascade.AllDeleteOrphan();
}
}
Ok, I have Saved a Teacher with a clases in the list of clases. All elements are saved in the database but the clases elements have in the database the attribute Teacher_Id as null. So the relation is not saved. Is possible to use an unidirectional relationship one to many and get the foreing key saved???????
is this behaivor normal?
second test:
public class Classes : I_DTO
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Teacher teacher { get; set; }
public Classes()
{
teacher = new Teacher();
}
}
public class Classes_Map : IAutoMappingOverride<Classes>
{
public void Override(AutoMapping<Classes> mapping)
{
mapping.References(x => x.teacher).Column("Teacher_Id");
}
}
Now I have been able to get the target but in this way:
Teacher profe = new Teacher();
profe.Name = "Peter";
List<Classes> classes = new List<Classes>();
Classes clase1 = new Classes();
clase1.Name = "Class1";
clase1.teacher = profe;
classes.Add(clase1);
profe.Classes = classes;
DAO_Result resultado = SQL_DAO_Service.Add<Teacher>(profe);
I this bidirectional example, I have to add to the teacher the clasess, and each class have to has as well the reference of the teacher to se that in the save it is able to generate all properly
is this behaviour correct?
And last but not least
If I have do and update of this teacher and and 2 clases with him, and after that I remove one of the element of the classes list, and I update the teacher, the system is not able to know that What I want is to delete the class from the database, so i will have to check on my own if the list of clases does not have and ald element to delete it...
is this correct?
thanks a lot!!!
best regards...
Thank you very much for your patient and your answer.I do not know If i have understud properly the concept of this: " Both sides of the now one to many need to be saved or updated first. "For instance, I have created a 2 new tables to do the tests. Teachers and Classes. They have only the attitubutes Id and Name. And I have added a relation one to many from teacher to classes.
public class Classes : I_DTO{
public virtual int Id { get; set; }
public virtual string Name { get; set; }public Classes(){}
}
namespace ExCon_DAO_SQL.DTO.Wall{public class Teacher : I_DTO{
public virtual int Id { get; set; }
public virtual string Name { get; set; }public virtual IList<Classes> Classes { get; set; }public Teacher(){Classes = new List<Classes>();}}
public class Teacher_Map : IAutoMappingOverride<Teacher>{public void Override(AutoMapping<Teacher> mapping){mapping.HasMany(x => x.Classes).LazyLoad().Inverse().AsBag().ForeignKeyConstraintName("Teacher_Id").Cascade.AllDeleteOrphan();}}
Ok, I have Save a Teacher with a clases in the list of clases. Both elements are saved in the database but the clases has in the database the element Teacher_Id as null
is this behaivor normal?second test:
public class Classes : I_DTO{
public virtual int Id { get; set; }
public virtual string Name { get; set; }public virtual Teacher teacher { get; set; }public Classes(){teacher = new Teacher();}
}public class Classes_Map : IAutoMappingOverride<Classes>{public void Override(AutoMapping<Classes> mapping){mapping.References(x => x.teacher).Column("Teacher_Id");}}Now I have been able to get the target but in this way:Teacher profe = new Teacher();profe.Name = "Peter";List<Classes> classes = new List<Classes>();Classes clase1 = new Classes();clase1.Name = "Class1";clase1.teacher = profe;classes.Add(clase1);profe.Classes = classes;DAO_Result resultado = SQL_DAO_Service.Add<Teacher>(profe);
I have to add to the teacher the clasess, and each class have to has as well the reference of the teacher to se that in the save it is able to generate all properlyis this behaviour correct?And last but not leastIf I have do and update of this teacher and and 2 clases with him, and after that I remove one of the element of the classes list, and I update the teacher, the system is not able to know that What I want is to delete the class from the database, so i will have to check on my own if the list of clases does not have and ald element to delete it...is this correct?thanks a lot!!!best regards...