【问题标题】:Fluent Nhibernate HasManyToMany both sides with noop map流利的 Nhibernate HasManyToMany 双方与 noop 地图
【发布时间】:2010-11-17 16:16:03
【问题描述】:

我正在尝试将 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(){}          
    }

BehavioralEventClassMap:

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); 
    }
}

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate poco domain-model


    【解决方案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)。

    【讨论】:

      【解决方案2】:

      如果您不需要来自 Behavior,则不要添加映射。 如果您需要并且不想进行操作,请使用 Cascade.None()

      【讨论】:

        猜你喜欢
        • 2010-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多