Home > Software engineering >  Ef creates foreign key property in shadow state
Ef creates foreign key property in shadow state

Time:09-27

I have database structure with one to many relationship, and need to declare string field as primary key. But EF creates the foreign key property 'Property name' in shadow state. As a result I get other Property name1 column.

Database entities:

public class Account
{
    public int Id { get; set; }

    public string AccountName { get; set; }

    public List<Contact> Contacts { get; set; } = new();

    public string IncidentName { get; set; }

    public Incident Incident { get; set; }
}

public class Incident
{    
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string IncidentName { get; set; }

    public List<Account> Accounts { get; set; }
}

Message:

The foreign key property 'Account.IncidentName1' was created in shadow state because a conflicting property with the simple name 'IncidentName' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type.

How could I fix that?

CodePudding user response:

There is one to one relationship between Account and Incident, IncidentName repeating twice. Cannot same property name in One to one. So you have to change name of the one of both.

public class Incident
{    
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string IncidentId { get; set; }

    public List<Account> Accounts { get; set; }
}
  • Related