Onother problem with Entity Framework 6. I don't know why, but I have some trouble to get back my object frome the database when I queue 2 one-to-many relationships.
My plain objects
public class Plan
{
public int id { get; set; }
public int largeur { get; set; }
public int longueur { get; set; }
public string nom { get; set; }
public virtual List<Etage> listEtages { get; set; }
public Plan() { }
}
public class Etage
{
public int id { get; set; }
public virtual List<PositionModule> listPositionModule { get; set; }
public virtual Plan plan { get; set; }
public Etage() { }
}
public class PositionModule
{
public int id { get; set; }
public int x1 { get; set; }
public int x2 { get; set; }
public int y1 { get; set; }
public int y2 { get; set; }
public string lineId { get; set; }
public virtual Module module { get; set; }
public virtual Etage etage { get; set; }
public PositionModule() { }
}
public class Module
{
public int id { get; set; }
public string libe { get; set; }
public string coupePrincipe { get; set; }
public virtual TModule typeModule { get; set; }
public decimal prix { get; set; }
public Module()
{
}
}
Ef6 fluent mapping
public class PlanConfiguration : EntityTypeConfiguration<Plan>
{
public PlanConfiguration()
{
ToTable("Plan");
HasKey<int>(a => a.id);
Property<int>(a => a.largeur).IsRequired();
Property<int>(a => a.longueur).IsRequired();
Property(a => a.nom).HasColumnType("varchar").HasMaxLength(50);
}
}
public class EtageConfiguration : EntityTypeConfiguration<Etage>
{
public EtageConfiguration()
{
ToTable("Etage");
HasKey<int>(a => a.id);
HasRequired<Plan>(x => x.plan).WithMany(x => x.listEtages);
}
}
public class PositionModuleConfiguration : EntityTypeConfiguration<PositionModule>
{
public PositionModuleConfiguration()
{
ToTable("PositionModule");
HasKey<int>(a => a.id);
HasRequired<Module>(a => a.module);
HasRequired<Etage>(x => x.etage).WithMany(x => x.listPositionModule);
Property<int>(x => x.x1);
Property<int>(x => x.x2);
Property<int>(x => x.y1);
Property<int>(x => x.y2);
Property(a => a.lineId).HasColumnType("varchar").HasMaxLength(30);
}
}
public class ModuleConfiguration : EntityTypeConfiguration<Module>
{
public ModuleConfiguration()
{
ToTable("Module");
HasKey<int>(a => a.id);
HasOptional<TModule>(a => a.typeModule);
Property(a => a.libe).HasColumnType("varchar").HasMaxLength(150);
Property(a => a.coupePrincipe).HasColumnType("varchar");
}
}
At the moment I'm able to store a Plan which has a list of Etage with many PositionModule. But when I want to get back my all plan with a get by id, the listEtages is empty.
By checking on the database, all foreign keys are good and I use the one-to-many with two other (simplier) objects and it works fine...
It's my first project with EF6 so if you have any tips to share, it will be a pleasure.
Thanks
Update
My DTOs
public class PlanDTO
{
public int id { get; set; }
public int largeur { get; set; }
public int longueur { get; set; }
public string nom { get; set; }
public List<EtageDTO> lesEtages { get; set; }
public PlanDTO()
{
lesEtages = new List<EtageDTO>();
}
}
public class EtageDTO
{
public int id { get; set; }
public List<PositionModuleDTO> lesModules { get; set; }
public PlanDTO plan { get; set; }
public EtageDTO()
{
lesModules = new List<PositionModuleDTO>();
plan = new PlanDTO();
}
}
public class PositionModuleDTO
{
public int id { get; set; }
public int x1 { get; set; }
public int x2 { get; set; }
public int y1 { get; set; }
public int y2 { get; set; }
public string lineId { get; set; }
public ModuleDTO module { get; set; }
public EtageDTO etage { get; set; }
public PositionModuleDTO()
{
module = new ModuleDTO();
}
}
public class ModuleDTO
{
public string libe { get; set; }
public int id { get; set; }
public string coupePrincipe { get; set; }
public TModule typeModule { get; set; }
}
How I mapp my DTOs and Plain objects (with automapper)
--- ViewModelToDomain ---
CreateMap<PlanDTO, Plan>()
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.largeur, map => map.MapFrom(vm => vm.largeur))
.ForMember(g => g.longueur, map => map.MapFrom(vm => vm.longueur))
.ForMember(g => g.nom, map => map.MapFrom(vm => vm.nom))
.ForMember(g => g.listEtages, map => map.MapFrom(vm => vm.lesEtages));
CreateMap<EtageDTO, Etage>()
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.listPositionModule, map => map.MapFrom(vm => vm.lesModules))
.ForMember(g => g.plan, map => map.MapFrom(vm => vm.plan));
CreateMap<PositionModuleDTO, PositionModule>()
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.x1, map => map.MapFrom(vm => vm.x1))
.ForMember(g => g.x2, map => map.MapFrom(vm => vm.x2))
.ForMember(g => g.y1, map => map.MapFrom(vm => vm.y1))
.ForMember(g => g.y2, map => map.MapFrom(vm => vm.y2))
.ForMember(g => g.module, map => map.MapFrom(vm => vm.module))
.ForMember(g => g.etage, map => map.MapFrom(vm => vm.etage));
CreateMap<ModuleDTO, Module>()
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.libe, map => map.MapFrom(vm => vm.libe))
.ForMember(g => g.typeModule, map => map.MapFrom(vm => vm.typeModule))
.ForMember(g => g.coupePrincipe, map => map.MapFrom(vm => vm.coupePrincipe));
--- DomainToViewModel ---
CreateMap<Plan, PlanDTO>();
CreateMap<Etage, EtageDTO>();
CreateMap<PositionModule, PositionModuleDTO>();
CreateMap<Module, ModuleDTO>();
The controller where I create and try to get back my plan
[HttpPost]
public ActionResult SavePlan(PlanDTO plan)
{
if (plan != null)
{
Plan planP = new Plan();
plan.nom = "test";
planP=Mapper.Map<PlanDTO, Plan>(plan);
try
{
_planService.Create(planP);//The plan is create
/*
refers to
public virtual void Insert(T entity)
{
dbSet.Add(entity);
}
*/
_planService.Save();
}
catch(Exception e)
{
throw (e);
}
return Json("Success");
}
else
{
return Json("An Error Has occoured");
}
}
[HttpPost]
public JsonResult GetPlan(int id)
{
try
{
List<ModuleDTO> lesModules = Mapper.Map<List<Module>, List<ModuleDTO>>(_moduleService.DonneTous().ToList());
PlanDTO plan = Mapper.Map<Plan, PlanDTO>(_planService.Get(id));//I have id, largeur, longueure and nom but listEtages is empty (all data are in database)
/*
refers to
public virtual T GetById(int id)
{
return dbSet.Find(id);
}
*/
return Json(plan);
}
catch(Exception e)
{
return Json("An Error Has occoured");
}
}