Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架类问题中的两个相同类类型_C#_Entity Framework_Entity Framework 4.1 - Fatal编程技术网

C# 实体框架类问题中的两个相同类类型

C# 实体框架类问题中的两个相同类类型,c#,entity-framework,entity-framework-4.1,C#,Entity Framework,Entity Framework 4.1,我正在实现允许用户相互跟踪的功能。 我有数据库表: User{UserId, FirstName, LastName etc.} Followings{FollowerUserId, FollowingUserId, CreatedOnDate etc.} 所以我添加了EF类: public class Follow { [Key, Column(Order = 1)] public Guid FollowerUserId { get; set; }

我正在实现允许用户相互跟踪的功能。 我有数据库表:

User{UserId, FirstName, LastName etc.}
Followings{FollowerUserId, FollowingUserId, CreatedOnDate etc.}
所以我添加了EF类:

public class Follow
    {
        [Key, Column(Order = 1)]
        public Guid FollowerUserId { get; set; }
        [Key, Column(Order = 2)]
        public Guid FollowUserId { get; set; }        
        public DateTime CreatedOnDate { get; set; }

        public virtual User Follower { get; set; }
        public virtual User Following { get; set; }
    }
最后两个虚拟财产问题。 当我打电话时:

var model = con.Follows.Where(x => x.FollowerUserId == uid);
我得到以下例外:

Invalid column name 'Following_UserId'.
这个问题可能是由一个类中的两个用户对象引起的。知道如何解决这个问题吗?
更新

public class User
    {

        public Guid UserId { get; set; }
        ...
        public virtual ICollection<Follow> Following { get; set; }
        public virtual ICollection<Follow> Followers { get; set; }
    }
公共类用户
{
公共Guid用户标识{get;set;}
...
在{get;set;}之后的公共虚拟ICollection
公共虚拟ICollection跟随者{get;set;}
}

我认为原因是外键属性(
FollowerUserId
FollowUserId
)和导航属性(
Follower
Follower
)不遵守命名约定,因此EF无法将第一个属性识别为外键。您可以通过使用
[ForeignKey]
属性显式指定FK属性来解决此问题:

public class Follow
{
    [Key, Column(Order = 1), ForeignKey("Follower")]
    public Guid FollowerUserId { get; set; }
    [Key, Column(Order = 2), ForeignKey("Following")]
    public Guid FollowUserId { get; set; }        

    public DateTime CreatedOnDate { get; set; }

    public virtual User Follower { get; set; }
    public virtual User Following { get; set; }
}
编辑

至少第二个属性不遵守命名约定,第一个看起来还可以。因此,您也可以通过将第二个FK属性
FollowUserId
重命名为:

public Guid FollowingUserId { get; set; }        
…因为导航属性在之后被称为

编辑2

关于您的更新:您需要添加
[InverseProperty]
属性来告诉EF哪些导航属性属于一起:

public class Follow
{
    [Key, Column(Order = 1), ForeignKey("Follower")]
    public Guid FollowerUserId { get; set; }
    [Key, Column(Order = 2), ForeignKey("Following")]
    public Guid FollowUserId { get; set; }        

    public DateTime CreatedOnDate { get; set; }

    [InverseProperty("Followers")]  // refers to Followers in class User
    public virtual User Follower { get; set; }
    [InverseProperty("Following")]  // refers to Following in class User
    public virtual User Following { get; set; }
}

我似乎不适合同时放置userID和navigation属性,您应该只放置其中一个,否则EF无法建立它们之间的连接。您能更好地解释一下吗?我不确定如何修复此问题?谢谢,这在后续课程中解决了我的问题。还有一件事,如果这不是一个问题,我还试图将以下者/追随者的集合添加到用户类中(我在问题更新中添加了它),我再次遇到类似的错误:无效的列名“User\u UserId”。