简体   繁体   English

实体框架自引用实体

[英]Entity Framework self referencing entity

I have a problem with the Entity Framework. 我对实体框架有疑问。

public class User : Receiver
{
    public User()
    {
        if (Groups == null)
            Groups = new List<Group>();
        if (Buddies == null)
            Buddies = new List<User>();
    }

    [Required]
    public string PhoneNumber { get; set; }
    [ForeignKey("Guid"), JsonIgnore]
    public IList<User> Buddies { get; set; }
    [ForeignKey("Guid"), JsonIgnore]
    public IList<Group> Groups { get; set; }
}

public class Receiver
{
    public Receiver()
    {
        Guid = Guid.NewGuid();
        Created = DateTime.Now;
    }

    [Key]
    public Guid Guid { get; set; }
    [Required]
    public DateTime Created { get; set; }
}

When i try to add a user... 当我尝试添加用户时...

User user = new User
            {
                Guid = new Guid("8cd094c9-e4df-494e-b991-5cf5cc03d6e3"),
                PhoneNumber = "+4991276460"
            };

        cmc.Receivers.Add(user);

... it ends in follogwing error. ...它以以下错误结束。

The object of the Type "System.Collections.Generic.List`1[Project.Models.User]" can't be converted to "Project.Models.User". 类型“ System.Collections.Generic.List`1 [Project.Models.User]”的对象不能转换为“ Project.Models.User”。

When i comment out following two lines: 当我注释掉以下两行时:

[ForeignKey("Guid"), JsonIgnore]
    public IList<User> Buddies { get; set; }

...the programm runs fine. ...程序运行正常。

I hope someone can help me to fix this problem. 我希望有人可以帮助我解决此问题。

Otherwise it runs into an error at this line : cmc.Receivers.Add(user); 否则,它将在此行发生错误: cmc.Receivers.Add(user);

In your mapping... 在您的映射中...

[ForeignKey("Guid"), JsonIgnore]
public IList<User> Buddies { get; set; }

...you specify that User.Buddies is part of a one-to-many relationship and that User.Guid (= Receiver.Guid ) is the foreign key in this relationship. ...您指定User.Buddies是一对多关系的一部分,并且User.Guid (= Receiver.Guid )是此关系中的外键。 But User.Guid is also the primary key, hence it must be unique. 但是User.Guid也是主键,因此它必须是唯一的。 As a result a User cannot have a list of Buddies but only a single reference. 结果, User不能有一个Buddies列表,而只能有一个参考。

The mapping makes no sense but the exception is not very helpful and difficult to understand. 映射没有意义,但是异常不是很有帮助,很难理解。 (Somehow EF seems to recognize internally that the Buddies cannot be a list with that mapping and wants to cast the list to a single reference. It should detect in my opinion that the mapping is invalid in the first place.) (以某种方式,EF似乎在内部认识到Buddies不能是具有该映射的列表,并且希望将该列表转换为单个引用。在我看来,它应该首先检测到该映射无效。)

For a correct one-to-many mapping you need a foreign key that is different from the primary key. 为了正确的一对多映射,您需要一个与主键不同的外键。 You can achieve that by either removing the [ForeignKey] annotation altogether... 您可以通过完全删除[ForeignKey]批注来实现...

[JsonIgnore]
public IList<User> Buddies { get; set; }

...in which case EF will create a default foreign key in the Receivers table (it will be some column with an underscore in its name, but you can rename that with Fluent API if you don't like the default name) or by adding your own foreign key property to the User class: ...在这种情况下,EF会在Receivers表中创建一个默认外键(它将是一个带有下划线的列,但是如果您不喜欢默认名称,则可以使用Fluent API重命名)将自己的外键属性添加到User类:

public Guid? BuddyGuid { get; set; }

[ForeignKey("BuddyGuid"), JsonIgnore]
public IList<User> Buddies { get; set; }

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

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