Automapping a Composite Model with Composite Iteration with FluentNhibernate

怎甘沉沦 提交于 2020-01-05 12:06:10

问题


I have a tree structured model and designed it with composite Pattern. for iterating through the entire hierachy Im using Composite Iteration. I have used this tutorial:

http://www.blackwasp.co.uk/Composite.aspx

but when I want to AutoMap the model, I encounter this problem:

{"The entity '<GetEnumerator>d__0' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id)."}

but getEnumerator is a method. I don't know why handle this like an Entity!!

public IEnumerator<MenuComponent> GetEnumerator()
        {
             foreach (MenuComponent child in menuComponents)
                yield return this;
        }

here is my AutoMapping Configuration :

public class AutomappingConfiguration: DefaultAutomappingConfiguration
    {
        //As we do not explicitly map entities or value objects, we define conventions or exceptions 
        //for the AutoMapper. We do this by implementing a configuration class.

        //this method instructs the AutoMapper to consider only those classes for mapping 
        //which reside in the same namespace as the Employeeentity.
        public override bool ShouldMap(Type type)
        {
           return type.Namespace == typeof(Menu).Namespace;

        }


    }

Uploaded the sample code:

public abstract class CombatElement
{
    public virtual string Name { get; set; }
    public virtual Guid Id { get; set; }

    public virtual void Add(
        CombatElement element)
    {
        throw new NotImplementedException();
    }

    public virtual void
        Remove(CombatElement element)
    {
        throw new NotImplementedException();
    }

    public virtual
        IEnumerable<CombatElement>
            GetElements()
    {
        throw new NotImplementedException();
    }



    public abstract void Fight();
    public abstract void Move();
}

//////

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Diagnostics;

namespace FluentNHibernateMvc3.Models

{
    public class Formation : CombatElement
    {
        private List<CombatElement> _elements;
        public virtual IEnumerable<CombatElement> Elements { get { return _elements; } }

        public Formation()
    {
        _elements = new List<CombatElement>();
    }

    public override void Add(
        CombatElement element)
    {
        _elements.Add(element);
    }

    public override void
        Remove(CombatElement element)
    {
        _elements.Remove(element);
    }

    public override void Fight()
    {
        Debug.WriteLine(this.Name + " Formation is fighting");
    }

    public override void Move()
    {
        Debug.WriteLine(this.Name + " Formation is moving");
    }

    public override
        IEnumerable<CombatElement>
            GetElements()
    {
        // yield up this current object first
        yield return this;

        // iterate through all child elements
        foreach (CombatElement fe in
            _elements)
        {
            // + iterate through each of its elements
            foreach (CombatElement feInner
                    in fe.GetElements())
                yield return feInner;
        }
    }
}

}

/////////

public class Soldier : CombatElement
{
    public virtual int Rank { get; set; }

    public override void Fight()
    {
        Debug.WriteLine(this.Name + " soldier is fighting");
    }

    public override void Move()
    {
        Debug.WriteLine(this.Name + " soldier is fighting");
    }

    public override
        IEnumerable<CombatElement>
            GetElements()
    {
        yield return this;
    }
}

and here how I create session factory

 // Returns our session factory
    private static ISessionFactory CreateSessionFactory()
    {
        //m => m.FluentMappings.AddFromAssemblyOf<FormationMap>()
        return Fluently.Configure()
            .Database( CreateDbConfig )
            .Mappings(m => m.AutoMappings.Add(CreateMappings()))
            .ExposeConfiguration( UpdateSchema )
            .CurrentSessionContext<WebSessionContext>()
            .BuildSessionFactory();
    }

    // Returns our database configuration
    private static MsSqlConfiguration CreateDbConfig()
    {
        return MsSqlConfiguration
            .MsSql2008
            .ConnectionString( c => c.FromConnectionStringWithKey( "testConn" ) );
    }

    // Returns our mappings
    private static AutoPersistenceModel CreateMappings()
    {
        var cfg = new AutomappingConfiguration();
        return AutoMap
            .Assemblies(cfg,System.Reflection.Assembly.GetCallingAssembly()).IncludeBase<CombatElement>()
            .Conventions.Setup( c => c.Add( DefaultCascade.SaveUpdate() ) );
    }

    // Updates the database schema if there are any changes to the model,
    // or drops and creates it if it doesn't exist
    private static void UpdateSchema( Configuration cfg )
    {
        new SchemaUpdate( cfg )
            .Execute( false, true );
    }

Does anyone has any idea?

来源:https://stackoverflow.com/questions/19189185/automapping-a-composite-model-with-composite-iteration-with-fluentnhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!