0

I am being struggling for few days now...I have two entities in different dbContext referencing the same table. When i try to run the project and try accessing the entities either by selecting data or inserting data it gives me error as

The entity types cannot share table 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.

I tried with the [ForeignKey("")] attribute and the virtual property but was not successfull. Following are my two entities, please help me as where am i going wrong.

 [Table("JHAContent")]
public partial class JHAContent
{

    [Key]
    public int ID { get; set; }

    [StringLength(50)]
    public string Name { get; set; }

    [StringLength(50)]
    public string ReferenceType { get; set; }

    [StringLength(50)]
    public string ReferenceID { get; set; }

    [StringLength(50)]
    public string ContentType { get; set; }

    public byte[] BlobContent { get; set; }

    public DateTime CreatedOn { get; set; }



}

and the second entity is as follows

 [Table("JHAContent")]
public partial class SourceJHAContent
{
    [Key, ForeignKey("JHAContent")]
    public int ID { get; set; }

    [StringLength(50)]
    public string Name { get; set; }

    [StringLength(50)]
    public string ReferenceType { get; set; }

    [StringLength(50)]
    public string ReferenceID { get; set; }

    [StringLength(50)]
    public string ContentType { get; set; }

    public byte[] BlobContent { get; set; }

    public DateTime CreatedOn { get; set; }


    public Weatherford.PumpJackOnline.DataModels.PumpJackEntityModels.JHAContent JHAContent { get; set; }
}

Please let me know how to get this code running. As i don't want to keep any relation between this two entities as they are exact copy of one another..one will be used to fetch details and other to insert details in the table.

vidyasagar85
  • 131
  • 1
  • 2
  • 11
  • Possible duplicate of [Entity Framework Table Splitting: not in the same type hierarchy / do not have a valid one to one foreign key relationship](http://stackoverflow.com/questions/24454193/entity-framework-table-splitting-not-in-the-same-type-hierarchy-do-not-have-a) – Igor Mar 15 '17 at 16:16
  • When you say "I have two entities in different dbContext referencing the same table", do you mean 2 instances of the same DbContext class, or 2 instances of 2 different DbContext classes? – Bradley Uffner Mar 15 '17 at 16:43
  • 2 different dbContext.... – vidyasagar85 Mar 15 '17 at 17:36
  • @BradleyUffner...i have 2 different dbContext and they have entities which are exactly the same and are pointing to the same table – vidyasagar85 Mar 15 '17 at 17:45

1 Answers1

0

Why do you need JHAContent field in SourceJHAContent class? That's what causes the issue, just remove it and everything should be fine.

Lanorkin
  • 7,310
  • 2
  • 42
  • 60