[英]Fluent NHibernate: How to map FK keys as properties in .NET Core version
we are using fluent API and I need to know how I can map it correctly so we can use ParentId instead of having to go through Parent entity.我们正在使用流畅的 API,我需要知道如何正确映射它,以便我们可以使用 ParentId 而不必通过 Parent 实体。 Is that possible?
那可能吗? I know it can be done using EF Core so I was hoping for a similar solution.
我知道可以使用 EF Core 来完成,所以我希望有一个类似的解决方案。
public class Parent
{
public int Id {get;set;}
public ISet<Child> Children {get;set;}
}
public class Child{
public int Id {get;set;}
public int ParentId {get;set;}
public Parent Parent {get;set;}
}
public static void Main()
{
var dummyQuery = Enumerable.Empty<Child>();
//Want to do like this.
dummyQuery= dummyQuery.Where(c => c.ParentId == 80).ToArray();
//This will do a join to the parent table.
dummyQuery= dummyQuery.Where(c => c.Parent.Id == 80).ToArray();
}
public class ChildMap : ClassMapping<Child>
{
public ChildMap ()
{
Table("Children");
//how do I do here?
ManyToOne(x => x.Parent, map => map.Column("ParentId"));
}
}
public class Parent
{
public virtual int Id { get; set; }
public virtual ISet<Child> Children { get; protected set; } = new HashSet<Child>();
}
public class Child
{
public virtual int Id { get; set; }
public virtual Parent Parent { get; set; }
}
public class ParentMap : ClassMapping<Parent>
{
public ParentMap()
{
Id(p => p.Id);
Set(p => p.Children, m => m.Inverse(true), m => m.OneToMany());
}
}
public class ChildMap : ClassMapping<Child>
{
public ChildMap()
{
Id(c => c.Id);
ManyToOne(c => c.Parent, map => map.Column("ParentId"));
}
}
// query
var res = session.Query<Child>().Where(c => c.Parent.Id == 80).ToArray();
With this mapping i get the following sql通过此映射,我得到以下 sql
select child0_.Id as id1_1_, child0_.ParentId as parentid2_1_ from Child child0_ where child0_.ParentId=@p0;@p0 = 80 [Type: Int32 (0:0:0)]
no need for parentId at all.根本不需要 parentId。 Lazy loading was maybe disabled.
延迟加载可能已禁用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.