Cha*_*vig 4 join fluent-nhibernate
我有几张桌子,例如:
我希望能够在我的Product对象上包含ManufacturerName(而不必在我只需要名称时加载整个Manufacturer行).我的ProductMap看起来像......
Table("Product");
Id(x => x.Id, "Id");
Map(x => x.ProductName, "ProductName");
Map(x => x.ManufacturerId, "ManufacturerId");
References(x => x.Manufacturer, "ManufacturerId");
Run Code Online (Sandbox Code Playgroud)
我需要添加什么来填充Product对象上的ManufacturerName属性?我相信我需要进行某种Join()调用,但是我无法弄清楚如何使用所有相关参数来编写它.它需要将当前表(Product)连接到Manufacturer表,在Product.ManufacturerId = Manufacturer.Id上,并获取Manufacturer.Name列,在对象上填充ManufacturerName属性.
我认为您可以使用a formula来动态检索制造商名称.这不是一个优雅的解决方案,我个人更喜欢使用一个单独的sql视图映射到一个新的实体(例如ProductExtra等),它只会查询必要的列,但无论如何.开始了:
将该新属性的映射行添加到ProductMap:
Table("Product");
Id(x => x.Id, "Id");
Map(x => x.ProductName, "ProductName");
Map(x => x.ManufacturerId, "ManufacturerId");
Map(x => x.ManufacturerName).Formula("(select m.ManufacturerName from Manufacturer m where m.Id = ManufacturerId)");
References(x => x.Manufacturer, "ManufacturerId");
Run Code Online (Sandbox Code Playgroud)希望这可以帮助.