简体   繁体   English

Ef 在影子 state 中创建外键属性

[英]Ef creates foreign key property in shadow state

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.但是 EF 在影子 state 中创建了外键属性“属性名称”。 As a result I get other Property name1 column.结果,我得到了其他Property name1列。

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.外键属性“Account.IncidentName1”是在影子 state 中创建的,因为实体类型中存在具有简单名称“IncidentName”的冲突属性,但要么未映射,要么已用于其他关系,要么与关联的不兼容主键类型。

How could I fix that?我怎么能解决这个问题?

There is one to one relationship between Account and Incident , IncidentName repeating twice. AccountIncident之间存在一对一的关系, IncidentName重复两次。 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; }
}

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

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