1

我有一个树结构模型,并用复合模式设计它。使用复合迭代遍历整个层次结构。我用过这个教程:

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

但是当我想自动映射模型时,我遇到了这个问题:

{"实体 '<GetEnumerator>d__0' 没有映射 Id。使用 Id 方法映射您的身份属性。例如:Id(x => x.Id)。"}

但 getEnumerator 是一种方法。我不知道为什么要像实体一样处理这个!!

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

这是我的自动映射配置:

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;

        }


    }

上传示例代码:

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

以及我如何创建会话工厂

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

有人知道吗?

4

0 回答 0