1

Entity Framework 6 的另一个问题。我不知道为什么,但是当我排队 2 个一对多关系时,我很难从数据库中取回我的对象​​。

我的普通对象

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 流利的映射

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");
        }
    }

目前,我可以存储一个包含 Etage 列表和许多 PositionModule 的计划。但是当我想通过 id 取回我的所有计划时,listEtages 是空的。

通过检查数据库,所有外​​键都很好,我将一对多与其他两个(更简单的)对象一起使用,它工作正常......

这是我第一个使用 EF6 的项目,所以如果您有任何提示要分享,那将是一种乐趣。

谢谢

更新

我的 DTO

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; }
    }

我如何映射我的 DTO 和普通对象(使用自动映射器)

--- 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>();

我创建并尝试取回我的计划的控制器

 [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");
            }


        }
4

0 回答 0