我的实体框架有问题。
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; }
}
当我尝试添加用户时...
User user = new User
{
Guid = new Guid("8cd094c9-e4df-494e-b991-5cf5cc03d6e3"),
PhoneNumber = "+4991276460"
};
cmc.Receivers.Add(user);
...它以以下错误结束。
“System.Collections.Generic.List`1[Project.Models.User]”类型的对象无法转换为“Project.Models.User”。
当我注释掉以下两行时:
[ForeignKey("Guid"), JsonIgnore]
public IList<User> Buddies { get; set; }
...程序运行良好。
我希望有人可以帮助我解决这个问题。
否则它会在这一行遇到错误:cmc.Receivers.Add(user);