停止使用edmx,使用Code First开始出现此错误

时间:2015-11-25 15:43:42

标签: entity-framework code-first edmx

所以在这里工作时我们决定停止使用edmx并开始使用Code First,主要是因为一遍又一遍地合并edmx文件的问题。

使用文件 - >新商品 - > Ado.Net实体数据模型我已经从我们现有的数据库中创建了模型,但是当运行一些代码时我得到了这个错误 我已经搜索过但无法弄清楚他们先说的是什么 错误行324,343根本不存在于所提到的类中 既不在生成的DBContext上。

这也应该开箱即用......

  

错误(3015):从第324,343行开始映射片段中的问题:   外键约束   ' SubModule_SubModulesCurricularUnitsAcademicYears'从表   SubModulesCurricularUnitsAcademicYear(ModuleId,SubModuleId)到表   SubModule(Id,ModuleId)::映射不足:外键必须是   映射到参与外部的一些AssociationSet或EntitySets   概念方面的关键关联。

     

ERROR(3015):从第99,135行开始映射片段时出现问题:   外键约束   ' PlanStudiesCurricularUnit_PlanStudiesCurricularUnitsAcademicYears'   来自表PlanStudiesCurricularUnitsAcademicYear(PlanStudyId,   CurricularUnitId)到表PlanStudiesCurricularUnit   (CurricularUnitId,PlanStudyId)::映射不足:外键   必须映射到参与的一些AssociationSet或EntitySets   概念方面的外键关联。

  CREATE TABLE [dbo].[SubModulesCurricularUnitsAcademicYears] (
    [PlanStudyId]      BIGINT         NOT NULL,
    [CurricularUnitId] BIGINT         NOT NULL,
    [AcademicYearId]   BIGINT         NOT NULL,
    [ModuleId]         BIGINT         NOT NULL,
    [SubModuleId]      BIGINT         NOT NULL,
    [TeacherId]        BIGINT         NOT NULL,

     ....
    CONSTRAINT [PK_SubModulesModulesCurricularUnitsAcademicYear] PRIMARY KEY CLUSTERED ([PlanStudyId] ASC, [CurricularUnitId] ASC, [AcademicYearId] ASC, [ModuleId] ASC, [SubModuleId] ASC, [TeacherId] ASC),
    CONSTRAINT [FK_SubModulesModulesCurricularUnitsAcademicYear_AcademicYears] FOREIGN KEY ([AcademicYearId]) REFERENCES [dbo].[AcademicYears] ([Id]),
    CONSTRAINT [FK_SubModulesModulesCurricularUnitsAcademicYear_CurricularUnits] FOREIGN KEY ([CurricularUnitId]) REFERENCES [dbo].[CurricularUnits] ([Id]),
    CONSTRAINT [FK_SubModulesModulesCurricularUnitsAcademicYear_PlanStudies] FOREIGN KEY ([PlanStudyId]) REFERENCES [dbo].[PlanStudies] ([Id]),
    CONSTRAINT [FK_SubModulesModulesCurricularUnitsAcademicYear_SubModules] FOREIGN KEY ([SubModuleId], [ModuleId]) REFERENCES [dbo].[SubModules] ([Id], [ModuleId]),
    CONSTRAINT [FK_SubModulesModulesCurricularUnitsAcademicYear_Users] FOREIGN KEY ([TeacherId]) REFERENCES [dbo].[Users] ([Id])
);




 CREATE TABLE [dbo].[SubModules] (
    [Id]          BIGINT         IDENTITY (1, 1) NOT NULL,
    [ModuleId]    BIGINT         NOT NULL,
    [Name]        NVARCHAR (150) NOT NULL,
    [Description] NVARCHAR (100) NULL,
    [Active]      BIT            CONSTRAINT [DF__SubModule__Activ__7A721B0A] DEFAULT ((1)) NOT NULL,
    [Deleted]     BIT            CONSTRAINT [DF_SubModules_Deleted] DEFAULT ((0)) NOT NULL,
    CONSTRAINT [PK_SubModules] PRIMARY KEY CLUSTERED ([Id] ASC, [ModuleId] ASC),
    CONSTRAINT [FK_SubMudules_Modules] FOREIGN KEY ([ModuleId]) REFERENCES [dbo].[Modules] ([Id]) ON DELETE CASCADE

生成的数据库上下文:

//onModelCreating only refers to Submodules and SubModulesCurricularUnitsAcademicYears in the following lines
...
protected override void OnModelCreating(DbModelBuilder modelBuilder){
   modelBuilder.Entity<SubModule>()
                .HasMany(e => e.SubModulesCurricularUnitsAcademicYears)
                .WithRequired(e => e.SubModule)
                .HasForeignKey(e => new { e.SubModuleId, e.ModuleId })
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<SubModulesCurricularUnitsAcademicYear>()
                .Property(e => e.ECTS)
                .HasPrecision(5, 2);

}
...

生成poco&#39>

public partial class SubModulesCurricularUnitsAcademicYear
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long PlanStudyId { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long CurricularUnitId { get; set; }

    [Key]
    [Column(Order = 2)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long AcademicYearId { get; set; }

    [Key]
    [Column(Order = 3)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long ModuleId { get; set; }

    [Key]
    [Column(Order = 4)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long SubModuleId { get; set; }

    [Key]
    [Column(Order = 5)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long TeacherId { get; set; }

    ...

    public virtual AcademicYear AcademicYear { get; set; }

    public virtual CurricularUnit CurricularUnit { get; set; }

    public virtual PlanStudy PlanStudy { get; set; }

    public virtual SubModule SubModule { get; set; }

    public virtual User User { get; set; }
}


public partial class SubModule
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public SubModule()
    {
        SubModulesCurricularUnitsAcademicYears = new HashSet<SubModulesCurricularUnitsAcademicYear>();
    }

    [Key]
    [Column(Order = 0)]
    public long Id { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long ModuleId { get; set; }

    ...


    public virtual Module Module { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<SubModulesCurricularUnitsAcademicYear> SubModulesCurricularUnitsAcademicYears { get; set; }
}

0 个答案:

没有答案