使用实体框架多重关系问题

时间:2018-11-30 14:56:52

标签: entity-framework ef-code-first c#-2.0

我正在尝试学习如何使用Entity Framework,但是我却陷入了困惑的境地。

我正在尝试建立一个包含4个表的数据库,这些表应该相互之间具有关系。而且我遇到了问题(错误消息如下所示)。

[Table("VERSION")]
public partial class Version
{
        public Version()
        {            
            O2cmoduls = new HashSet<O2cmodul>();
        }

        [Key]
        [Column("VERSID")]
        public int VersId { get; set; }

        public virtual ICollection<O2cmodul> O2cmoduls { get; set; }
}

[Table("O2CMODUL")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification = "Suppressed by Entity Framework")]
public class O2cmodul
{
            public O2cmodul()
            {
                O2cmodulProducts = new HashSet<O2cmodulProduct>();
            }

            [Key]       
            [Column("VERSID",Order =1)]
            public int VersId { get; set; }

            [Key]
            [Column("MODULID",Order =2)]
            [StringLength(15)]
            public string ModulId { get; set; }

            [Column("MODULTEXT")]
            public string ModulText { get; set; }

            [ForeignKey("VersId")]
            public virtual Version Version { get; set; }        

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "Suppressed by Entity Framework")]
            public virtual ICollection<O2cmodulProduct> O2cmodulProducts { get; set; }
}

[Table("O2CMODUL_PRODUCT")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification = "Suppressed by Entity Framework")]
public class O2cmodulProduct
{
        public O2cmodulProduct()
        {
            O2cmodulNutzungs = new HashSet<O2cmodulNutzung>();
        }

        [Key]
        [Column("VERSID",Order =1)]
        public int VersId { get; set; }

        [Key]
        [Column("MODULIDMODULID",Order = 2)]
        [StringLength(15)]
        public string ModulId { get; set; }

        [Key]
        [Column("PRODUCTID",Order = 3)]
        [StringLength(15)]
        public string ProductId { get; set; }

        [Column("PRODUCTTEXT")]
        [StringLength(60)]
        public string ProductText { get; set; }

        [ForeignKey("VersId")]
        public virtual Version Version { get; set; }
        [ForeignKey("ModulId")]
        public virtual O2cmodul O2cmodul { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "Suppressed by Entity Framework")]
        public virtual ICollection<O2cmodulNutzung> O2cmodulNutzungs { get; set; }
}

[Table("O2CMODUL_NUTZUNG")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification = "Suppressed by Entity Framework")]
public class O2cmodulNutzung
{
        [Key]
        [Column("VERSID",Order = 1)]
        public int VersId { get; set; }

        [Key]
        [Column("MODULID",Order = 2)]
        [StringLength(15)]
        public string ModulId { get; set; }

        [Key]
        [Column("PRODUCTID", Order = 3)]
        [StringLength(15)]
        public string ProductId { get; set; }

        [Key]
        [Column("NUTZUNGSSZENARIO", Order = 4)]
        [StringLength(30)]
        public string UseScenario { get; set; }

        [Column("SWITCHON")]
        public string SWITCHON { get; set; }       

        [Column("QTD_CUTOMIZED")]
        public int QTDCustomized { get; set; }

        [ForeignKey("VersId")]
        public virtual Version Version { get; set; }
        [ForeignKey("ModulId")]
        public virtual O2cmodul O2cmodul { get; set; }
        [ForeignKey("ProductId")]
        public virtual O2cmodulProduct O2cmodulProduct { get; set; }

}

错误的代码-我试图这样映射它:

public class DatabaseContext : DbContext
{
        public virtual DbSet<O2cmodul> O2cmoduls { get; set; }
        public virtual DbSet<O2cmodulProduct> O2cmodulProducts { get; set; }
        public virtual DbSet<O2cmodulNutzung> O2cmodulNutzungs { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //#region[Version]
            //modelBuilder.Entity<Version>()
            //   .HasOptional(e => e.O2cmoduls)
            //  .WithRequired(e => e.VersionID)
            //  .Map(e => e.MapKey(VERSID));
            //#endregion

            //#region [O2cmodul]

        //modelBuilder.Entity<O2cmodul>()
        //   .HasOptional(e => e.O2cmodulProduct)
        //   .WithRequired(e => e.VersionID)
        //   .Map(e => e.MapKey(VERSID));

        //modelBuilder.Entity<O2cmodul>()
        //   .HasOptional(e => e.O2cmodulProduct)
        //   .WithRequired(e => e.ModulID)
        //   .Map(e => e.MapKey(MODULID));

        //#endregion

        //#region [O2cmodulProduct]

        //modelBuilder.Entity<O2cmodulProduct>()
        //  .HasOptional(e => e.O2cmodulNutzung)
        //  .WithRequired(e => e.VersionID)
        //  .Map(e => e.MapKey(VERSID));

        //modelBuilder.Entity<O2cmodulProduct>()
        //   .HasOptional(e => e.O2cmodulNutzung)
        //   .WithRequired(e => e.ModulID)
        //   .Map(e => e.MapKey(MODULID));

        //modelBuilder.Entity<O2cmodulProduct>()
        //   .HasOptional(e => e.O2cmodulNutzung)
        //   .WithRequired(e => e.ProductID)
        //   .Map(e => e.MapKey(PRODUCTID));

        //#endregion
        }
}

这里是结构:

数据库中的现有表[Version]

VersId int (PK)

[O2cmodul]

VersId int (PK,FK)
ModulId string (PK)
ModulText string

[O2cmodulProduct]

VersId int (PK,FK)
ModulId string (PK,FK)
ProductId string (PK)
ProductText string

[O2cmodulNutzung]

VersId int (PK,FK)
ModulId string (PK,FK)
ProductId string (PK,FK)
UseScenario string (PK)
switchon string
QTDcutomized int

关系:

[Version] 1:n [O2cmodul] 1:n [O2cmodulProduct] 1:n [O2cmodulNutzung]

我收到此错误:

enter image description here

  

在模型生成过程中检测到一个或多个验证错误:

     

O2cmodulProduct_O2cmodul_Target_O2cmodulProduct_O2cmodul_Source::关系约束中的从属角色和主体角色的属性数必须相同。

     

O2cmodulNutzung_O2cmodul_Target_O2cmodulNutzung_O2cmodul_Source::关系约束中从属角色和主体角色的属性数必须相同。

     

O2cmodulNutzung_O2cmodulProduct_Target_O2cmodulNutzung_O2cmodulProduct_Source::关系约束中从属角色和主体角色的属性数量必须相同。

我希望这不会造成混淆。

感谢帮助。

0 个答案:

没有答案