多对多问题CTP4代码优先 [英] Many to Many Problem CTP4 Code First

查看:81
本文介绍了多对多问题CTP4代码优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 


以下是我收到的错误:


表'Part_Size'被映射为连接表对于'MISMobileDAL.WOs.WOPartsList'的导航属性'Sizes'以及对象集'WOPartsListSet'。导航属性必须与对象集内联或映射到单独的表。


 


此Part_Size表是一个多对多的连接表,它定义了零件可能具有的每个尺寸以及该零件/尺寸的相关属性,例如库存水平和定价......


 

     [ DataContract (IsReference  =   true )]     [ Serializable ]      public    class   ;  WOPartSize  
     {

          [ DataMember ]         公共  int   WOPartSizeId  {  get set ; }         [ DataMember ]         public   DateTime   tadded  {  get set ; }
         [ DataMember ]
          [ MaxLength (50)]
         public   string   size  {  get set ; }
      &nb sp;  [ DataMember ]         [ MaxLength (20)]
         public   string   type  {  get 设置; }         [ DataMember ]         public   int   WOPersonnelId  {  get set ; }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; [<跨度风格= "颜色:#2b91af">数据成员]
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;   public   virtual   < span style ="color:#2b91af"> WOPersonnel
  AddedBy  {  get 设置; }
         // 导航 收藏         [ DataMember ]
          public   virtual   ICollection的<的 WOPartsList >&NBSP;零件与NBSP; {&NBSP;的 GET set ; }

       &NBSP;&NBSP;的 //&NBSP;查找&NBSP;关于&NBSP;所述 电话&NBSP;这些&NBSP;字段&NBSP;提供&NBSP;所述&NBSP; snyc&NBSP;机制
&NBSP;&NBSP;&NBSP ;      [ DataMember ]         public   DateTime   LastEditDate  {  get 设置; }         [ DataMember ]
         public   DateTime   CreationDate  {  get set ; }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; [<跨度风格= "颜色:#2b91af">数据成员]
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;   [ StoreGenerated StoreGeneratedPattern .None)]   ;       publi c   Guid   rowguid  {  get set ; }

    }

     public   class   WOPartSizeEntityConfig &NBSP;:&NBSP;的 EntityConfiguration <的 WOPartSize >
&NBSP;&NBSP;&NBSP ;  {
         public   WOPartSizeEntityConfig()
  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 .HasKey(E&NBSP; =>&NBSP; e.WOPartSizeId);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; .Property(e  =>  e.WOPartSizeId).IsIdentity();

             //  FK              this .HasRequired(e => e.AddedBy).HasConstraint((e, p) => e.WOPersonnelId == p.WOPersonnelId);

        }
    }

>
     [ DataContract ]
     [可序列化]     public   class   Part_Size
     {
          [密钥]
      &nbs p;  [ DataMember ]         public   int   WOPartSizeMMId  {  get ;  < span style ="color:blue"> set
; }         [ DataMember ]         public   日期时间  tadded  {  get set ;           [ DataMember ]
  ;        [ MaxLength (50)]
          public   string & nbsp; part_no  {  get set ; }  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; [<跨度风格= "颜色:#2b91af">数据成员]
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   public   string   part_descr  {  获取设置; }         [< span style ="color:#2b91af"> DataMember
]         [ MaxLength (50)]         public   string   sku  {  get set ;           [ DataMemb呃]         public   十进制  cost_each  {  get set ;  ;}         [ DataMember ]
          public   decimal   price_each  {  < span style ="color:blue"> get
set ; }

          [ DataMember ]
         public   int   on_hand  {  get set ; }
      & nbsp;  [ DataMember ]         public   int   on_trucks  {  get ;  < span style ="color:blue"> set
; }         [ DataMember ]         public   int   on_order  {  get set ;  ;}         // 导航 参考         [ DataMember ]    ;       public   virtua l   WOPartsList   Part  {  get set ; }         [ DataMember ]         public   int   WOPartListId  {  get set ;  }         [ DataMember ]
   ;       public   virtual   WOPersonnel   AddedBy  {  get set ; }         [ DataMember ]         public   int   WOPersonnelId  {  get set ; }         [ DataMember ]
         public   virtual   ; WOPartSize   PartSize  {  get 设置; }         [ DataMember ]
         public   int   WOPartSizeId  {  get set ; }来自        //&NBSP;查找&NBSP;关于&NBSP;所述 电话&NBSP;这些&NBSP;字段&NBSP;提供&NBSP;所述&NBSP; snyc&NBSP;机制
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; [<跨度style ="color:#2b91af"> DataMember
]         public   DateTime   LastEditDate  {  get 设置; }         [ DataMember ]
         public   DateTime   CreationDate  {  get set ; }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; [ DataMember ]         [ StoreGenerated StoreGeneratedPattern .None)]         公开  Guid   rowguid  {  get set ; }绿色;   }


 

     [ DataContract ( IsReference  =  true )]     [ Serializable ]     public   class   WOPartsList
     {

          [ DataMember ]
         public   int   WOPartsListId  {  get set ; }         [ DataMember ]         public   DateTime   tadded  {  get set ; }来自       [ DataMember ]         public   字符串  part_descr  {  get set ; }

         // 导航 参考         [ DataMember ]         public   int   WOPersonnelId  {  get set ;  ;}         [ DataMember ]
          public   virtual   WOPersonnel   AddedBy  {  get set ; }         [ Da taMember ]         public   虚拟  WOPartType   PartType  {  get ;  ; set ; }         [ DataMember ]         public   int   WOPartTypeId  {  get set ; }         [ DataMember ]
         public   virtual   ICollection < WOPartSize > 大小  {  get set ; }
         // 查找   电话 这些  ;字段&NBSP;提供&NBSP;所述&NBSP; snyc&NBSP;机制
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; [<跨度风格= "颜色:#2b91af">数据成员]         public   DateTime   LastEditDate  {  get set ; }
         [ DataMember ]      ;    public   DateTime   CreationDate  {  获得;&NBSP; < span style ="color:blue"> set ; }         [ DataMember ]         [ StoreGenerated StoreGeneratedPattern .None)]
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的公共&NBSP ; Guid   rowguid  {  get 设置; }

    }

     public   class   WOPartsListEntityConfig  :  < span style ="color:#2b91af"> EntityConfiguration
< WOPartsList >
     {
         公共&NBSP; WOPartsListEntityConfig()
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 .HasKey(E&NBSP; =>&NBSP; e.WOPartsListId);
&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 .Property(E&NBSP; =>&NBSP; e.WOPartsListId) .IsIdentity();             //  ;定义&NBSP;所述&NBSP; FK&NBSP; relaitionships
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 .HasRequired(E&NBSP; =>&NBSP; e.PartType).HasConstraint((E,&NBSP; T)&NBSP; =>&NBSP; e.WOPartTypeId&NBSP; ==&NBSP; t.WOPartTypeId);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 .HasRequired(E&NBSP; => &NBSP; e.AddedBy).HasC onstraint((e,  p)  =>  e.WOPersonnelId  ==  p.WOPersonnelId);

      ;   }

    }


 


我尝试使用流畅的API进行如下配置:


<预风格= "font-family:索拉"> <预风格= "font-family:索拉">&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; modelBuilder.Entity<的 WOPartsList >()的hasMany(E&NBSP; =>&NBSP; e.Sizes).WithMany(E&NBSP; =>&NBSP,E。部分)。地图(" Part_Size" , (p,  s)  =>     ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的&NBSP; {&NBSP; PART_ID&NBSP; =&NBSP; p.WOPartsListId,  size_id  =  s.WOPartSizeId });

   &nbs磷;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; modelBuilder.Entity<的 Part_Size >()MapSingleType(E&NBSP; =>   new
             {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; cost_each&NBSP; =&NBSP; e.cost_each,
&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; CreationDate&NBSP; =&NBSP; e.CreationDate,
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; LastEditDate&NBSP; =&NBSP; e.LastEditDate,
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; on_hand&NBSP; =&NBSP; e.on_hand,
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; on_order&NBSP; =&NBSP; e.on_order,
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NB属;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; on_trucks&NBSP; =&NBSP; e.on_trucks,
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NB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&NBSP; e.WOPersonnelId <无线电通信/>            }).ToTable(" Part_Size" );;



 


我做错了什么?


 


谢谢


 Greg 


 


 


 


 

 


&nbs p;




谢谢,Greg

解决方案

嗨Greg,


实体框架通常不支持包含有效负载的多对多表(即除了两个外键之外的任何东西),意味着在Code First中映射的多对多关系将只有两个类的集合,并且
中没有类来表示连接表。


在您的示例中,您需要将其映射为两个独立的关系:多个关系WOPartsList - > Part_Size和WOPartSize - > WOPartsList。


~Rowan


 

Here is the error that I am getting:

The table 'Part_Size' is mapped as a join table for the navigation property 'Sizes' of 'MISMobileDAL.WOs.WOPartsList' and also to an object set 'WOPartsListSet'. The navigation property must be mapped inline with the object set or to a separate table.

 

This Part_Size table is a many to many join table that defines each size a part may have along with associated properties for that part/size such as inventory levels and pricing...

 

    [DataContract(IsReference = true)]
    [Serializable]
    public class WOPartSize
    {

        [DataMember]
        public int WOPartSizeId { getset; }
        [DataMember]
        public DateTime tadded { getset; }
        [DataMember]
        [MaxLength(50)]
        public string size { getset; }
        [DataMember]
        [MaxLength(20)]
        public string type { getset; }

        [DataMember]
        public int WOPersonnelId { getset; }
        [DataMember]
        public virtual WOPersonnel AddedBy { getset; }

        // Nav Collections
        [DataMember]
        public virtual ICollection<WOPartsList> Parts { getset; }


        // Lookups on the Phone these fields provide the snyc mechanism
        [DataMember]
        public DateTime LastEditDate { getset; }
        [DataMember]
        public DateTime CreationDate { getset; }
        [DataMember]
        [StoreGenerated(StoreGeneratedPattern.None)]
        public Guid rowguid { getset; }

    }

    public class WOPartSizeEntityConfig : EntityConfiguration<WOPartSize>
    {
        public WOPartSizeEntityConfig()
        {
            this.HasKey(e => e.WOPartSizeId);
            this.Property(e => e.WOPartSizeId).IsIdentity();

            // FK's
            this.HasRequired(e => e.AddedBy).HasConstraint((e, p) => e.WOPersonnelId == p.WOPersonnelId);


        }
    }



    [DataContract]
    [Serializable]
    public class Part_Size
    {
        [Key]
        [DataMember]
        public int WOPartSizeMMId { getset; }
        [DataMember]
        public DateTime tadded { getset; }

        [DataMember]
        [MaxLength(50)]
        public string part_no { getset; }
        [DataMember]
        public string part_descr { getset; }
        [DataMember]
        [MaxLength(50)]
        public string sku { getset; }

        [DataMember]
        public decimal cost_each { getset; }
        [DataMember]
        public decimal price_each { getset; }

        [DataMember]
        public int on_hand { getset; }
        [DataMember]
        public int on_trucks { getset; }
        [DataMember]
        public int on_order { getset; }



        // Nav Refs
        [DataMember]
        public virtual WOPartsList Part { getset; }
        [DataMember]
        public int WOPartListId { getset; }

        [DataMember]
        public virtual WOPersonnel AddedBy { getset; }
        [DataMember]
        public int WOPersonnelId { getset; }

        [DataMember]
        public virtual WOPartSize PartSize { getset; }
        [DataMember]
        public int WOPartSizeId { getset; }


        // Lookups on the Phone these fields provide the snyc mechanism
        [DataMember]
        public DateTime LastEditDate { getset; }
        [DataMember]
        public DateTime CreationDate { getset; }
        [DataMember]
        [StoreGenerated(StoreGeneratedPattern.None)]
        public Guid rowguid { getset; }


    }



    [DataContract(IsReference = true)]
    [Serializable]
    public class WOPartsList
    {

        [DataMember]
        public int WOPartsListId { getset; }
        [DataMember]
        public DateTime tadded { getset; }


        [DataMember]
        public string part_descr { getset; }


        // Nav References

        [DataMember]
        public int WOPersonnelId { getset; }
        [DataMember]
        public virtual WOPersonnel AddedBy { getset; }

        [DataMember]
        public virtual WOPartType PartType { getset; }
        [DataMember]
        public int WOPartTypeId { getset; }

        [DataMember]
        public virtual ICollection<WOPartSize> Sizes { getset; }


        // Lookups on the Phone these fields provide the snyc mechanism
        [DataMember]
        public DateTime LastEditDate { getset; }
        [DataMember]
        public DateTime CreationDate { getset; }
        [DataMember]
        [StoreGenerated(StoreGeneratedPattern.None)]
        public Guid rowguid { getset; }

    }

    public class WOPartsListEntityConfig : EntityConfiguration<WOPartsList>
    {

        public WOPartsListEntityConfig()
        {
            this.HasKey(e => e.WOPartsListId);
            this.Property(e => e.WOPartsListId).IsIdentity();

            // Define the FK relaitionships
            this.HasRequired(e => e.PartType).HasConstraint((e, t) => e.WOPartTypeId == t.WOPartTypeId);
            this.HasRequired(e => e.AddedBy).HasConstraint((e, p) => e.WOPersonnelId == p.WOPersonnelId);


        }

    }


I tried to use the fluent API to configure as follows:

            modelBuilder.Entity<WOPartsList>().HasMany(e => e.Sizes).WithMany(e => e.Parts).Map("Part_Size", (p, s) =>
                new { part_id = p.WOPartsListId, size_id = s.WOPartSizeId });

            modelBuilder.Entity<Part_Size>().MapSingleType(e => new
            {
                cost_each = e.cost_each,
                CreationDate = e.CreationDate,
                LastEditDate = e.LastEditDate,
                on_hand = e.on_hand,
                on_order = e.on_order,
                on_trucks = e.on_trucks,
                part_descr = e.part_descr,
                part_no = e.part_no,
                price_each = e.price_each,
                sku = e.sku,
                tadded = e.tadded,
                WOPartListId = e.WOPartListId,
                WOPartSizeId = e.WOPartSizeId,
                WOPartSizeMMId = e.WOPartSizeMMId,
                WOPersonnelId = e.WOPersonnelId
            }).ToTable("Part_Size");


What am I doing wrong ?


Thanks

Greg





 

 


Thanks, Greg

解决方案

Hi Greg,

Entity Framework in general does not support many to many tables that contain payload (i.e. anything other than the two foreign keys), meaning many to many relationships mapped in Code First will just have a collection on the two classes and no class in the middle to represent the join table.

In your example you would need to map this as two separate one:many relationships WOPartsList -> Part_Size and WOPartSize -> WOPartsList.

~Rowan


这篇关于多对多问题CTP4代码优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆