简体   繁体   中英

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.

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.

This is the history table 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:

[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.'

Any idea how to fix it?

Well at first glance, I think you need a foreign key in 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 . Also the table attribute over the TrxHistoryForecast class should have a value [Table("trx_history_forecast")] ie without the _tracking suffix. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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