代码之家  ›  专栏  ›  技术社区  ›  Chris Kooken

流畅的Nhibernate有很多两边都有noop地图

  •  0
  • Chris Kooken  · 技术社区  · 15 年前

    我正试图将POCO映射为多对多关系。我不想要包含BehavioralEvents的行为属性。我很确定多对多映射必须在这两个位置,但是我不希望在我的行为类上有相应的属性。

    我听说你可以使用一个禁止操作的接入运营商,但我不知道如何做到流利的Nhibernate。

    请告知:

    public class BehavioralEvent : AggregateRoot    
        {       
            protected internal IList<Behavior> Behaviors { get; private set; }
    
            public BehavioralEvent()
            {
                Behaviors = new List<Behavior>();
            }
        }
    

    行为类(没有对BehavioralEvent的引用)

    public class Behavior : AggregateRoot
    {
            protected internal virtual string Name { get; private set; }
            protected internal virtual string Definition { get; private set; }           
    
            public Behavior(string name, Guid id) 
            {
                this.Id = id;
                this.Name = name;               
            }
    
            protected Behavior(){}          
        }
    

    行为类映射:

    public class BehavioralEventClassMap : ClassMap<BehavioralEvent>
        {
            public BehavioralEventClassMap()
            {
                Id(x => x.Id, "BehavioralEventId").GeneratedBy.Assigned();
    
                HasManyToMany(x => x.Behaviors)
                    .Cascade.All()
                    .Table("BehaviorData")
                    .ParentKeyColumn("BehavioralEventId")
                    .ChildKeyColumn("BehaviorId");
            }
        }
    

    行为类映射:

    public class BehaviorClassMap : ClassMap<Behavior>
    {
        public BehaviorClassMap()
        {
            Table("Behaviors");
            Id(x => x.Id, "BehaviorId").GeneratedBy.Assigned();
            Map(x => x.Name).Not.Nullable();
            Map(x => x.Definition); 
        }
    }
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Falcon    15 年前

    你不需要从两边画地图。

    我有各种各样的映射,比如:

    HasManyToMany(x => x.SomeCollection).Table("MappingTable").ParentKeyColumn("ParentKey").ChildKeyColumn("ChildKey").Cascade.AllDeleteOrphan();
    

    很有魅力!将其映射为集合或集合(请参见 http://www.codinginstinct.com/2010/03/nhibernate-tip-use-set-for-many-to-many.html ).

        2
  •  0
  •   Jugal Panchal    12 年前

    如果不需要来自行为,则不需要外接程序映射。 如果您需要并且不想放置操作,请使用Cascade.None()

    推荐文章