0

我盯着 Linq,不知道如何处理这个查询。

我有一个 UserProfile 表。每个 UserProfile 可以有许多图像(UserImages 表)

我的模型(简化):

    public class UserProfile
{
    [Key]
    public int Id { get; set; }
    public string Name  { get; set; }
    public ICollection<Image> UsrImages { get; set; }
}

[Table("ImagesLocation")]
public class UserImages
{   //This is just an Id (and I don't even need it) It is not, from 
    public int Id { get; set; } what I can see, the UserProfile Id foreign key
    public string ImgPath { get; set; }
    public bool? ImgDefault { get; set; }
}

     public class UserProfileDBContext : DbContext
{
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<UserImages> userImages { get; set; }
 }

我想要一个类似于以下内容的 linq 查询:

select * from dbo.imgLocation where userProfile_id = 1 (or any user id)

这将为特定用户带来一个包含所有“图像”(或图像数据/路径)的列表。我看到 EF 自动在 UserImages 表上创建了一个名为 userProfile_id 的列,但我无法使用 linq 查询它,因为它不存在!

我一直在谷歌上搜索它,但我什么也找不到(添加我尝试过的东西没有意义!..最终,我可以改变我的模型来完成这项工作。我的主要问题是我找不到钥匙/与我的 UserImages 模型上的 UserProfile 相关的外键

关于如何做到这一点的任何提示?谢谢!... PnP

4

1 回答 1

1

为了访问该列,您需要声明一个标量属性和一个匹配的导航属性,以便 EF 可以按照约定检测关系的两端。

[Table("ImagesLocation")]
public class UserImages
{   
    public int Id { get; set; }

    public int UserProfileId { get; set; }
    public virtual UserProfile UserProfile { get; set; }

    public string ImgPath { get; set; }
    public bool? ImgDefault { get; set; }
}

然后您可以通过以下方式查询图像

var images = db.userImages.Where(i => i.UserProfileId == 1);
于 2012-08-03T00:36:44.263 回答