简体   繁体   English

EF6 ApplicationUser错误的多个外键

[英]EF6 Multiple foreign keys for ApplicationUser Error

I'm pretty new to Entity Framework and the code first approach and I'm stuck. 对于Entity Framework和代码优先方法,我还很陌生,但我陷入了困境。 Sorry if it's a dumb question. 抱歉,这是一个愚蠢的问题。

I found some questions around here that look the same, but I get an other error then the ones other people get and I would love to solve it without the need to add other parameters if possible. 我在这里发现了一些看起来相同的问题,但是遇到另一个错误,然后是其他人遇到的错误,我很乐意解决该问题,如果可能的话,无需添加其他参数。

So Basically, I have my ApplicationUser(load in from Identity) that looks like this: 所以基本上,我的ApplicationUser(从Identity加载)看起来像这样:

public class ApplicationUser : IdentityUser
{
    public virtual Province Provincie { get; set; }

    [ForeignKey("From")]
    public virtual ICollection<Message> SentMessages { get; set; }

    [ForeignKey("To")]
    public virtual ICollection<Message> ReceivedMessages { get; set; }


}

And I have a Message Class that looks like: 我有一个消息类,看起来像:

public class Message
{
    [Key]
    public int ID { get; set; }
    public virtual ApplicationUser From { get; set; }
    public virtual ApplicationUser To { get; set; }
    public String MessageContent { get; set; }
    public DateTime Date { get; set; }
}

Now, when I try to add a migration i get the following error: 现在,当我尝试添加迁移时,出现以下错误:

The ForeignKeyAttribute on property 'ReceivedMessages' on type 'EF_CF_Basics.Models.ApplicationUser' is not valid. 类型为'EF_CF_Basics.Models.ApplicationUser'的属性'ReceivedMessages'上的ForeignKeyAttribute无效。 The foreign key name 'To' was not found on the dependent type 'EF_CF_Basics.Models.Message'. 在从属类型“ EF_CF_Basics.Models.Message”上找不到外键名称“ To”。 The Name value should be a comma separated list of foreign key property names. Name值应该是逗号分隔的外键属性名称列表。

So actually, visual studio tells me it can't find the To in Message, but it is really there. 因此,实际上,Visual Studio告诉我找不到消息中的“收件人”,但确实存在。

You probably want to use [InverseProperty("From")] instead of [ForeignKey("From")] and [InverseProperty("To")] instead of [ForeignKey("To")] . 您可能想使用[InverseProperty("From")]代替[ForeignKey("From")][InverseProperty("To")]代替[ForeignKey("To")] Foreign key properties must be scalars ( int , string , Guid , etc.) but your From and To properties are actually entities, ie they are navigation properties. 外键属性必须是标量( intstringGuid等),但是FromTo属性实际上是实体,即它们是导航属性。

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

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