我有一个树状结构模型和复合模式设计的。 对通过整个层次结构迭代im使用复合迭代。 我已经使用这个教程:
http://www.blackwasp.co.uk/Composite.aspx
但是当我想要自动映射模型,我遇到这样的问题:
{ “实体 '<的GetEnumerator> d__0' 不具有一个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 );
}
有没有人有什么想法?