0

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

我听说您可以使用无操作访问运算符,但我不确定如何在 Fluent 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); 
    }
}
4

2 回答 2

1

您不需要从两侧映射它。

我有各种这样的映射:

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

奇迹般有效!将其映射为 Collection 或 Set(参见http://www.codinginstinct.com/2010/03/nhibernate-tip-use-set-for-many-to-many.html)。

于 2010-11-17T16:57:38.413 回答
0

If you do not need from Behavior then do not add in mapping. If you need and do not want to put operation then use Cascade.None()

于 2013-07-15T10:06:48.710 回答