简体   繁体   English

实体类型不能共享同一个表 (EF6)(一对多关系)

[英]Entity Types Cannot Share the same table (EF6) (One to Many Relationship)

This error comes out no matter how i tried to configure things.无论我如何尝试配置,都会出现此错误。 I was trying to build a one to many relationship (one history data to many tracking data) using EF6 and manually design the table with the relationship.我试图使用 EF6 建立一对多关系(一个历史数据到多个跟踪数据)并手动设计具有该关系的表。

This is the table design:这是表格设计:

在此处输入图像描述

As seen in the image, history tracking table has a column referencing the id in history table and the foreign key is generated.如图所示,历史跟踪表有一列引用历史表中的 id 并生成外键。

This is the history table class:这是历史表 class:

[Table("trx_history_forecast_tracking")]
    public class TrxHistoryForecast
    {
        public TrxHistoryForecast()
        {
            this.Tracking = new HashSet<TrxHistoryForecastTracking>();
        }

        [Key]
        [Column("id")]
        [Display(Name = "Id")]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int Id { get; set; }

        [Column("trx_id")]
        [Display(Name = "Transaction ID")]
        public string TransactonID { get; set; }

        [Column("current_activity")]
        [Display(Name = "Current Activity")]
        public string CurrentActivity { get; set; }

        [Column("send_date")]
        [Display(Name = "Send Date")]
        public DateTime SendDate { get; set; }

        [Column("user_create")]
        [Display(Name = "User Create")]
        public string UserCreate { get; set; }

        [Column("user_modify")]
        [Display(Name = "User Modified")]
        public string UserModified { get; set; }

        [Column("create_date")]
        [Display(Name = "Create Date")]
        public DateTime CreateDate { get; set; }

        [Column("modified_date")]
        [Display(Name = "Modified Date")]
        public DateTime? ModifyDate { get; set; }

        public ICollection<TrxHistoryForecastTracking> Tracking { get; set; }
    }

And this is the tracking class:这是跟踪 class:

[Table("trx_history_forecast_tracking")]
    public partial class TrxHistoryForecastTracking
    {
        [Key]
        [Column("id")]
        [Display(Name = "Id")]
        public int Id { get; set; }

        [Column("activity_name")]
        [Display(Name = "Activity Name")]
        public string ActivityName { get; set; }

        [Column("activity_finish_date")]
        [Display(Name = "Activity Finish Date")]
        public DateTime ActivityFinishDate { get; set; }

        [Column("activity_user")]
        [Display(Name = "Activity User")]
        public string ActivityUser { get; set; }

        [Column("activity_action")]
        [Display(Name = "Activity Action")]
        public string ActivityAction { get; set; }

        [Column("user_create")]
        [Display(Name = "User Create")]
        public string UserCreate { get; set; }

        [Column("create_date")]
        [Display(Name = "Create Date")]
        public DateTime CreateDate { get; set; }
        public virtual TrxHistoryForecast HistoryForecast { get; set; }
    }

However when i was trying to debug, this error comes out:但是,当我尝试调试时,出现此错误:

System.InvalidOperationException: 'The entity types 'TrxHistoryForecastTracking' and 'TrxHistoryForecast' cannot share table 'trx_history_forecast_tracking' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them.' System.InvalidOperationException:“实体类型‘TrxHistoryForecastTracking’和‘TrxHistoryForecast’不能共享表‘trx_history_forecast_tracking’,因为它们不在同一类型层次结构中,或者不具有有效的一对一外键关系以及它们之间匹配的主键。”

Any idea how to fix it?知道如何解决吗?

Well at first glance, I think you need a foreign key in TrxHistoryForecastTracking乍一看,我认为您需要 TrxHistoryForecastTracking 中的外键

public partial class TrxHistoryForecastTracking
{   
    .... the other properties/columns here....
    [Column(trx_history_forecast_tracking_id)]
    [ForeignKey("HistoryForecast ")]
    public int TrxHistoryForecastId { get; set; }
    public virtual TrxHistoryForecast HistoryForecast { get; set; }
}

This way a foreign key column will be created in the table trx_history_forecast_tracking .这样,将在表trx_history_forecast_tracking中创建一个外键列。 Also the table attribute over the TrxHistoryForecast class should have a value [Table("trx_history_forecast")] ie without the _tracking suffix.此外, TrxHistoryForecast class 上的表属性应具有值[Table("trx_history_forecast")] ,即没有_tracking后缀。 Now you are trying to create two entity classes that are pointing to the same database table.现在您正在尝试创建两个指向同一个数据库表的实体类。 You actually need to entity classes that map to two distinct tables in the database.您实际上需要将 map 实体类添加到数据库中的两个不同表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM