Question

Je suis en train de tracer une POCO comme plusieurs à plusieurs. Je ne veux pas une propriété sur le comportement qui contient BehavioralEvents. Je suis assez sûr que la mise en correspondance Beaucoup-to-Many doit être dans les deux endroits, mais je ne veux pas la propriété correspondante sur ma classe de comportement.

Je vous ai entendu pouvez utiliser un opérateur d'accès sans op, mais je ne suis pas sûr de savoir comment faire en Fluent NHibernate.

S'il vous plaît conseiller:

public class BehavioralEvent : AggregateRoot    
    {       
        protected internal IList<Behavior> Behaviors { get; private set; }

        public BehavioralEvent()
        {
            Behaviors = new List<Behavior>();
        }
    }

Classe de comportement (Aucune référence retour à 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");
        }
    }

BehaviorClassMap:

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); 
    }
}
Était-ce utile?

La solution

Vous n'avez pas besoin de mapper des deux côtés.

J'ai diverses applications comme ceci:

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

Fonctionne comme un charme! Carte comme Collection ou Set (voir http://www.codinginstinct.com/2010/03/nhibernate-tip-use-set-for-many-to-many.html ).

Autres conseils

Si vous n'avez pas besoin de comportement alors ne pas ajouter à la cartographie. Si vous avez besoin et ne veulent pas mettre l'opération puis utilisez Cascade.None ()

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top