实体框架6几个一对一

时间:2017-03-11 08:02:56

标签: entity-framework one-to-many fluent-entity-framework

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

目前,我能够存储一个包含许多PositionModule的Etage列表的计划。 但是当我想用id by get来取回我的所有计划时,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和Plain对象(使用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>();

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

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


        }

0 个答案:

没有答案