【问题标题】:EF error Code first error 3004EF错误代码第一个错误3004
【发布时间】:2015-11-23 00:05:19
【问题描述】:

我用 EF 设计器创建了这个简单的图表并生成了代码,但是当我生成解决方案时,我得到了这个错误:

Erreur 1 Erreur 3004:Problème de Fragments de mappage à partir de la ligne 84 :Aucun mappage n'est spécifié pour les propriétés intDB.tpintDB_id_tpint dans Jeu intDBs。 Une entite avec clé (PK) n'effectuera pas d'aller-retour lorsque : [Entité] 是类型 [helpdeskModel.intDB]

this is a link to an image of my diagram

代码如下:

    public partial class helpdeskEntities : DbContext
    {
        public helpdeskEntities()
            : base("name=helpdeskEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<intDB> intDBs { get; set; }
        public DbSet<tpintDB> tpintDBs { get; set; }
    }
}




     public partial class intDB
{
    public int ID { get; set; }
    public Nullable<System.DateTime> debint { get; set; }
    public Nullable<System.DateTime> finint { get; set; }
    public Nullable<int> id_int { get; set; }
    public decimal id_tpint { get; set; }
    [ForeignKey("id_tpint")]
    public virtual tpintDB tp_intDB { get; set; }
}
    }





      public partial class tpintDB
    {
        public decimal id_tpint { get; set; }
        public string libelle { get; set; }
        public string desc_tpint { get; set; }

        public virtual ICollection<intDB> intDBs { get; set; }
    }
    }

我的新 intDB 模型:

public partial class intDB
{
    public int ID { get; set; }
    public Nullable<System.DateTime> debint { get; set; }
    public Nullable<System.DateTime> finint { get; set; }
    public Nullable<int> id_int { get; set; }
    [ForeignKey("id_tpint")]
    public virtual int tp_intDB { get; set; }
}

【问题讨论】:

    标签: c# asp.net-mvc entity-framework mapping


    【解决方案1】:

    如果没有一段代码(即实体类),我们无法确定问题出在哪里。但问题对我来说似乎很明显:在 intdb 实体中,您需要此属性: public virtual tpintDB tpintDB { get; set; } 在 tpintDB 中你需要 public ICollection &lt;intDB&gt; intDBList { get; set; }。 此外,请确保您启用自动迁移或自行添加迁移。

    更新

    我更新了我的答案,以便您更好地了解应该如何声明实体。

    public partial class intDB
    {
        public int ID { get; set; }
        public Nullable<System.DateTime> debint { get; set; }
        public Nullable<System.DateTime> finint { get; set; }
        public Nullable<int> id_int { get; set; }
        public int id_tpint { get; set; }
        [ForeignKey("id_tpint")]
        public virtual tpintDB tp_intDB { get; set; }
    }
    

    更新 2

    另外,tpintDB 需要看起来像这样。

    public partial class tpintDB
    {
        public int id_tpint { get; set; }
        public string libelle { get; set; }
        public string desc_tpint { get; set; }
    
        public virtual ICollection<intDB> intDBs { get; set; }
    }
    

    将 ICollection 视为 tpintDB 记住哪些 intDB 对象引用它的工具。我的英语现在不是最好的,但我希望你能理解 :D 另外,我不保证这是最好的解决方案,但它适用于我在一个项目中的 30 多个实体,我认为它很干净。

    【讨论】:

    • 如何添加迁移?
    • 打开包管理器控制台并写add-migration [randomName]。就这么简单
    • 现在我有这个:错误 1 ​​错误 2039:概念属性“id_tpint”已经映射到类型为“int”的存储属性。如果概念属性映射到存储模型中的多个属性,请确保存储模型的所有属性都具有相同的类型。
    • 假设您在 intDB 实体中遇到此错误,您需要将 id_tpint 属性声明为 int,因为它将包含被引用实体的 id。此外,如果这不能解决此问题,请使用 DataAnnotations 告诉 EF 您希望作为 FK 具有什么属性。喜欢:[ForeignKey ("id_tpint")] 位于虚拟 tpintDB 属性上方。更多信息here
    • 它对我不起作用..我编辑了我的代码,你可以验证它是否正确
    【解决方案2】:

    请检查 Id_tpint 的 DataType 是否相同。 您还必须为 intDB 和 tpintDB 定义主键和外键 Id_tpint。

    【讨论】:

    • 我认为这是在类图中定义的......不是吗?
    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 2021-09-06
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多