我正在寻找一些实体时检索一些额外的数据。这是我到目前为止的位置。
Parts表将多次具有相同的Part(多个集合),当我尝试应用Migration时,HasForeignKey出现错误(e => new {e.Number,e.ColorId});因为它不是唯一的。还有另一种方法吗?
编辑:添加集时,不需要添加“ MyPart”记录。
// Tables
public class Set
{
public int Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public int NumberOfParts { get; set; }
public IList<Part> Parts
}
public class Part
{
public int Id { get; set; }
public string Number { get; set; }
public int ColorId { get; set; }
public int Quantity { get; set; }
public Set Set { get; set; }
}
public class MyParts
{
public int Id { get; set; }
public string Number { get; set; }
public int ColorId { get; set; }
public int Quantity { get; set; }
public string Location { get; set; }
}
/ Repo Method
public async Task<Set> GetSetAsync(string number, CancellationToken cancellationToken = default)
{
//My Call to get the data
var set = await Get<Set>()
Where(s => s.Number == number)
.Include(x => x.Parts)
.SingleOrDefaultAsync(cancellationToken);
return set;
}
我正在寻找MyParts.Location和MyParts.Quantity作为GetSetAsync()的一部分
我添加了
modelBuilder.Entity<Part>()
.HasOptional(e => e.MyPart) // I may not have the part
.WithOne()
.HasForeignKey(e => new { e.Number, e.ColorId });
所需查询
Select set.*,
part.*
myParts.Location,
myParts.Quantity
FROM Set set
INNER JOIN Part part on set.Id = part.setId
LEFT JOIN MyParts myParts on my Parts.ColorId = part.ColorId and myParts.Number = part.Number
预先感谢