Генерация программных карт с помощью Fluent NHibernate - PullRequest
1 голос
/ 29 июня 2011

Я начинаю с NHibernate и использую Fluent NHibernate. Я пытаюсь сопоставить класс с таблицей, где в таблице есть столбцы для каждого дня недели, например, "sunday_enabled", "monday_enabled", "tuesday_enabled" и т. Д.

Ниже моя попытка достичь этого.

public class WeekShiftTimingMapping : ClassMap<WeekShiftTiming>
{
    public WeekShiftTimingMapping()
    {
        Not.LazyLoad();

        Id(c => c.Id);

        References(x => x.Admin).Column("admin_id").LazyLoad().Cascade.All();

        foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek)))
        {
            Component(CreateExpression(day), CreateAction(day)); // Null Reference here
        }

        Table("reports_timing");
    }

    private Expression<Func<WeekShiftTiming, DayShiftTiming>> CreateExpression(DayOfWeek day)
    {
        return (x => x[day]);
    }

    private Action<ComponentPart<DayShiftTiming>> CreateAction(DayOfWeek day)
    {
        string dayName = DayName(day);

        return (m =>
        {
            m.Map(x => x.IsEnabled).Column(dayName + "_enabled").CustomSqlType("tinyint");
            m.Map(x => x.StartTime).Column(dayName + "_start");
            m.Map(x => x.EndTime).Column(dayName + "_end");
        });
    }

    private string DayName(DayOfWeek day)
    {
        return Enum.GetName(typeof(DayOfWeek), day).ToLowerInvariant();
    }

}

/// <summary>
/// A class which represents a week's shift times
/// </summary>
public class WeekShiftTiming 
{
    Dictionary<DayOfWeek, DayShiftTiming> dict;

    public WeekShiftTiming()
    {
        dict = new Dictionary<DayOfWeek, DayShiftTiming>();
        foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek)))
        {
            dict[day] = new DayShiftTiming();
        }
    }

    public virtual int Id { get; set; }
    public virtual Administrator Admin { get; set; }

    public virtual DayShiftTiming this[DayOfWeek day]
    {
        get { return dict[day]; }
        set { dict[day] = value; }            
    }

}

Я получаю исключение в указанной строке, пока выполняется Fluently.Configure () .... Может кто-нибудь сказать мне, что я делаю не так, или, может быть, есть лучший способ добиться того, что мне нужно?

FluentNHibernate.Cfg.FluentConfigurationException was unhandled by user code
Message=An invalid or incomplete configuration was used while creating a SessionFactory.
Check PotentialReasons collection, and InnerException for more detail.

Source=FluentNHibernate
StackTrace:
   at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 234
   at SimplytrakLibrary.DataAccess.SimplytrakDb.SessionFactory.CreateSessionFactory(String ConnectionString) in C:\Users\johncc\Documents\Projects\Services\EmailScheduler\trunk\SimplytrakLibrary\SimplytrakLibrary.DataAccess.SimplytrakDb\SessionFactory.cs:line 25
   at SimplytrakLibrary.Tests.TestSetup.GlobalInit() in C:\Users\johncc\Documents\Projects\Services\EmailScheduler\trunk\SimplytrakLibrary\SimplytrakLibrary.Tests\TestSetup.cs:line 23
InnerException: FluentNHibernate.Cfg.FluentConfigurationException
   Message=An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.


   Source=FluentNHibernate
   StackTrace:
        at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 263
        at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 227
   InnerException: System.Reflection.TargetInvocationException
        Message=Exception has been thrown by the target of an invocation.
        Source=mscorlib
        StackTrace:
             at System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo method, Object[] args, SignatureStruct& signature, RuntimeType declaringType)
             at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
             at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
             at FluentNHibernate.Utils.Extensions.InstantiateUsingParameterlessConstructor(Type type) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Utils\Extensions.cs:line 58
             at FluentNHibernate.PersistenceModel.Add(Type type) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\PersistenceModel.cs:line 132
             at FluentNHibernate.Utils.CollectionExtensions.Each[T](IEnumerable`1 enumerable, Action`1 each) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Utils\CollectionExtensions.cs:line 14
             at FluentNHibernate.PersistenceModel.AddMappingsFromSource(ITypeSource source) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\PersistenceModel.cs:line 88
             at FluentNHibernate.PersistenceModel.AddMappingsFromAssembly(Assembly assembly) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\PersistenceModel.cs:line 77
             at FluentNHibernate.Cfg.FluentMappingsContainer.Apply(PersistenceModel model) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentMappingsContainer.cs:line 120
             at FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\MappingConfiguration.cs:line 86
             at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 251
        InnerException: System.NullReferenceException
             Message=Object reference not set to an instance of an object.
             Source=FluentNHibernate
             StackTrace:
                  at FluentNHibernate.Utils.Reflection.ReflectionHelper.GetDynamicComponentProperty(Expression expression) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Utils\Reflection\ReflectionHelper.cs:line 83
                  at FluentNHibernate.Utils.Reflection.ReflectionHelper.GetMember(Expression expression) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Utils\Reflection\ReflectionHelper.cs:line 49
                  at FluentNHibernate.Utils.Reflection.ReflectionHelper.GetMember[TModel,TReturn](Expression`1 expression) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Utils\Reflection\ReflectionHelper.cs:line 14
                  at FluentNHibernate.Utils.ReflectionExtensions.ToMember[TMapping,TReturn](Expression`1 propertyExpression) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Utils\ReflectionExtensions.cs:line 11
                  at FluentNHibernate.Mapping.ClasslikeMapBase`1.Component[TComponent](Expression`1 expression, Action`1 action) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Mapping\ClasslikeMapBase.cs:line 266
                  at SimplytrakLibrary.DataAccess.SimplytrakDb.WeekShiftTimingMapping..ctor() in C:\Users\johncc\Documents\Projects\Services\EmailScheduler\trunk\SimplytrakLibrary\SimplytrakLibrary.DataAccess.SimplytrakDb\WeekShiftTiming.cs:line 24
             InnerException: 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...