迁移时与C#EF冲突

时间:2019-05-21 06:51:24

标签: c# entity-framework

我刚开始使用EF并创建了模型,并试图建立一种用户拥有多个设备的关系,我尝试了多个示例,但似乎无济于事。

课程:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public DateTime Created_at { get; set; }

    public  ICollection<Device> Devices { get; set; }
}

public class Device
{
    public int Id { get; set; }
    public string Name  { get; set; }
    public string Type { get; set; }
    public string Gateway { get; set; }
    public DateTime Created_at { get; set; }

    public int User_id { get; set; }
    public User User;

}

这些是类,正如我在文档中读到的那样,我认为必须在DbContext上进行编码:

HasMany(t => t.Devices)
            .WithRequired(t => t.User)
            .WillCascadeOnDelete(true);

但我仍然遇到此错误:

The expression 't => t.User' is not a valid property expression. The expression should represent a property: C#: 't => t.MyProperty'  VB.Net: 'Function(t) t.MyProperty'.

1 个答案:

答案 0 :(得分:1)

您的用户当前是字段,而不是属性。将其更改为此属性即可:

public User User { get; set; }