简体   繁体   English

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

[英]Two same class types in entity framework class issue

I am implementing feature that allows users to follow each other. 我正在实现允许用户互相关注的功能。 I have database tables: 我有数据库表:

User{UserId, FirstName, LastName etc.}
Followings{FollowerUserId, FollowingUserId, CreatedOnDate etc.}

So I added EF class: 所以我添加了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; }
    }

The last two virtual properties couse issue. 最后两个虚拟属性问题。 When I call: 我打电话的时候:

var model = con.Follows.Where(x => x.FollowerUserId == uid);

I get following exception: 我得到以下异常:

Invalid column name 'Following_UserId'.

The issue is probably caused because of two User objects in one class. 该问题可能是由于一个类中有两个User对象引起的。 Any idea how to workaround this? 知道如何解决这个问题吗?
UPDATE UPDATE

public class User
    {

        public Guid UserId { get; set; }
        ...
        public virtual ICollection<Follow> Following { get; set; }
        public virtual ICollection<Follow> Followers { get; set; }
    }

I think the reason is that the foreign key properties ( FollowerUserId and FollowUserId ) and navigation properties ( Follower and Following ) do not respect the naming conventions so that EF is unable to recognize the first properties as foreign keys. 我认为原因是外键属性( FollowerUserIdFollowUserId )和导航属性( FollowerFollowing )不遵守命名约定,因此EF无法将第一个属性识别为外键。 You can fix the problem by specifying the FK properties explicitly using the [ForeignKey] attribute: 您可以通过使用[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; }
}

Edit 编辑

A least the second property doesn't respect the naming convention, the first one looks OK. 至少第二个属性不遵守命名约定,第一个看起来没问题。 So, alternatively you can fix the problem by renaming the second FK property FollowUserId into: 因此,您也可以通过将第二个FK属性FollowUserId重命名为:

public Guid FollowingUserId { get; set; }        

...because the navigation property is called Following . ...因为导航属性称为Following

Edit 2 编辑2

About your UPDATE: You need to add the [InverseProperty] attribute to tell EF which navigation properties belong together: 关于您的更新:您需要添加[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; }
}

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

相关问题 实体框架:具有相同类问题的两个属性 - Entity Framework: Two propertes with same class issue 如何将相同 class 的两个列表拆分为实体框架中的不同表? - How to split two lists of the same class to different tables in Entity Framework? 无法确定类型之间关联的主要结束 - 实体框架错误,同一类之间的类关系 - Unable to determine the principal end of an association between the types - Entity Framework error, class relationship between the same class 实体框架-多个组成相同的类 - Entity framework - multiple composition to same class 实体框架中具有相同类的多个列表 - Multiple lists of same class in Entity Framework 实体框架代码优先-派生类与基类不在同一程序集中 - Entity Framework code first - Derived class not in the same assembly as the base class 实体框架 - Class 的列表与父级具有相同的 class - Entity Framework - List of Class having Same class to parent 实体框架代码类的第一个属性和相同类的列表 - Entity Framework Code First One Property of Class and List of The Same Class 类库,实体框架代码优先和Microsoft.SqlServer.Types - Class Library, Entity Framework Code First & Microsoft.SqlServer.Types 使用部分类将两个属性合并到实体框架实体 - Merge two properties to Entity Framework entity with partial class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM