实体框架类问题中有两个相同的类类型 [英] Two same class types in entity framework class issue

查看:106
本文介绍了实体框架类问题中有两个相同的类类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施功能,允许用户互相追踪。
我有数据库表:

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.}

所以我添加了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);

我收到以下异常:

Invalid column name 'Following_UserId'.

问题可能是由于一个类中的两个User对象引起的。任何想法如何解决这个问题?

更新

The issue is probably caused because of two User objects in one class. Any idea how to workaround this?
UPDATE

public class User
    {

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


推荐答案

外键属性( FollowerUserId FollowUserId )和导航属性(跟随者以下)不遵守命名约定,因此EF无法将第一个属性识别为外键。您可以通过使用 [ForeignKey] 属性明确指定FK属性来解决问题:

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. You can fix the problem by specifying the FK properties explicitly using the [ForeignKey] attribute:

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 重命名为:

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:

public Guid FollowingUserId { get; set; }        

...因为导航属性被称为跟随

...because the navigation property is called Following.

编辑2

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

About your UPDATE: You need to add the [InverseProperty] attribute to tell EF which navigation properties belong together:

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; }
}

这篇关于实体框架类问题中有两个相同的类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆