如何在一对多关系中指定哪列是外键,哪列是主键.使用实体框架核心



我有两个这样的表

public class Shepherd 
{
public long Id { get; set; }
public string Name { get; set; }
public string UserId{ get; set; }
}
public class Goat 
{
public long Id { get; set; }
public string Hairyness { get; set; }
public string CreatedBy { get; set; }
}

我知道如何使用Ids创建一对多关系,但我需要使用UserId和">CreatedBy"列创建从牧羊人到许多山羊的一对多的关系。我希望这是有道理的。提前非常感谢。

我找到了自己的解决方案!使用Linq。

var goatPoo = from Goats in _context.Goats
join Shepherd in _context.Shepherds
on Goats.CreatedBy equals Shepherd.UserId
where Shepherd.UserId == 77
select new { 
Shepherd.Id, 
Shepherd.Firstname, 
Shepherd.Lastname, 
Goats.Hairyness
};

最新更新