Mapping interface or abstract class component

前端 未结 4 2049
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 14:24

Please consider the following simple use case:

public class Foo 
{ 
    public virtual int Id { get; protected set; } 
    public virtual IBar Bar { get; set; }          


        
相关标签:
4条回答
  • 2021-02-10 14:50

    I haven't tried this myself but i saw an example in the fluent nh source where the lambda is cast to the concrete class:

    public class FooMap : ClassMap<Foo> 
    { 
        public FooMap() 
        { 
            Id(x => x.Id); 
            Component(x => (Bar) x.Bar, m => 
            { 
                m.Map(x => x.Text); 
            }); 
        } 
    }
    

    EDIT: Obviously that's exactly the same result as Sly's suggestion so that did no good. I tested it against the trunk version of fluent nh and it didn't work. It does work though if you use a many-to-one mapping:

      public class FooMap : ClassMap<Foo>
      {
        public FooMap()
        {
          Id(x => x.Id);
          References<Bar>(x => x.Bar).Cascade.All();
        }
      }
    
      public class BarMap : ClassMap<Bar>
      {
        public BarMap()
        {
          Id(x => x.Id);
          Map(x => x.Text);
        }
      }
    

    UPDATE

    This was actually an easy fix. I have submitted a patch (link text) that makes Sly's solution to work.

    0 讨论(0)
  • 2021-02-10 14:52

    Maybe this?

    public FooMap()
        {
            Id(x => x.Id);
            Component<Bar>(x => x.Bar, m =>
            {
                m.Map(x => x.Text);
            });
        }
    
    0 讨论(0)
  • 2021-02-10 14:55

    Here's a similar topic using the union-subclass approach, although the example does not use the fluent interface for the map:

    Click for article...

    0 讨论(0)
  • 2021-02-10 15:05

    Have not used the fluent NHibernate yet but seems to me what you are trying to do is map to an IUserType which you haven't told hibernate how to deal with. here is a useful example for defined usertypes in fluent.

    http://blog.jagregory.com/2009/01/11/fluent-nhibernate-auto-mapping-type-conventions/

    0 讨论(0)
提交回复
热议问题