[英]EF Core 5.0 relational from non-relational db
所以这就是问题所在。
我有一个来自旧 COBOL 软件(txt 文件)的古老数据库。
我想要的是
背景
数据库是 MS-SQL。 每个导入的 object 都有一个唯一的 id(数据库生成)。 旧 COBOL 软件中实际组合对象的“外键”(“NonUniqueItemProperty”)不是唯一的。 (这就是为什么我不能在表之间创建关系)
我的问题是,这甚至可能吗?
目前我加入查询表为
select item.*,owner.* from Items item
INNER JOIN Ownership ownership on item.NonUniqueItemProperty=ownership.NonUniqueItemProperty
INNER JOIN Owner owner on ownership.OwnerIdNo=owner.OwnerIdNo
此查询返回匹配结果集 (item-->ownerCount)*(item-->count)。 然后我使用 linq 按 Item.NonUniqueItemProperty 对它们进行分组,并为每个项目设置所有者。
对象:
public class Item
{
pullic int Id {get;set;} // database generated, non-related to original data
public string NonUniqueItemProperty {get;set;} //sadly this is the identifier
public virtual ICollection<Ownership> Ownerships { get; set; }
}
public class Ownership
{
pullic int Id {get;set;} // database generated, non-related to original data
public virtual Item Item { get; set; }
public virtual Owner Owner { get; set; }
public string NonUniqueItemProperty {get;set;}
public string OwnerIdNo {get;set;}
}
public class Owner
{
pullic int Id {get;set;} // database generated, non-related to original data
public virtual ICollection<Ownership> Ownerships { get; set; }
public string OwnerIdNo {get;set;}
}
我试图在流利的 api 建立关系:
item.HasMany<Ownership>(i => i.Ownerships)
.WithOne(ow => ow.Item)
.HasPrincipalKey(i=>i.NonUniqueItemProperty)
.HasForeignKey(ow=>ow.NonUniqueItemProperty);
就目前而言,没有错误,但导航属性为 null。
在此之后,我意识到这个表达式返回一个带有 null 所有权导航的 Item 实体。 结果集返回了我要查找的内容,因此导航可用于过滤项目集!
var resultSet = ctx.Items.Where(
i => i.Ownerships.Any(
ow => ow.Owner.OwnerIdNo=="P12345"));
我设法通过 Eager Loading 获得导航属性,并在此处解释
通过将Include(i=>i.Ownership).ThenInclude(ow=>ow.Owner)
添加到查询的末尾。
但是根据两位领域专家的建议,这不应该起作用,我不打算在代码中实现它,这在 EF 的更高版本中也不起作用。
这是一个真正的性能杀手,通过编写上面的 sql-query,TOP 150 项消耗 120-150 毫秒。 这个是 2.500-4.000 毫秒( query.Take(100)
)。
(项目数:350K,所有权数:390K)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.