迁移未启动 - Autofac会引发异常

时间:2014-02-04 23:20:39

标签: c# .net asp.net-mvc orchardcms autofac

我创建了一个模块来抑制依赖关系,而该依赖关系又压制了Orchard.Users.Services.MembershipService

当我尝试启用该模块时,果园崩溃时出现异常:

Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(...)' on type 'MembershipService'. 

内部例外

System.Data.SqlClient.SqlException: Invalid object name 'MyModule_MyModel'

当然,表格不会退出,但应按照Migrations.cs

的定义创建
using System;
using Orchard.Data;
using Orchard.Data.Migration;
using MyModule.Models;

namespace MyModule
{
    public class Migrations : DataMigrationImpl
    {
        private readonly IRepository<SettingsRecord> _repository;

        public Migrations(IRepository<SettingsRecord> repository)
        {
            _repository = repository;
        }

        public int Create()
        {
            SchemaBuilder.CreateTable("SettingsRecord", table => table
                    .Column<int>("Id", column => column.PrimaryKey().Identity())
                    .Column<string>("DefaultDomain")
                );

            return 1;
        }
    }
}

导致此迁移无法启动的原因是什么?

更新

public MembershipService(
        IRepository<SettingsRecord> settingsRepository,
        IRepository<DomainRecord> domainsRepository,
        IOrchardServices orchardServices,
        IMessageManager messageManager,
        IEnumerable<IUserEventHandler> userEventHandlers,
        IClock clock,
        IEncryptionService encryptionService
        )
    {
        _settingsRepository = settingsRepository;
        _domainsRepository = domainsRepository;
        _orchardServices = orchardServices;
        _messageManager = messageManager;
        _userEventHandlers = userEventHandlers;
        _encryptionService = encryptionService;
        Logger = NullLogger.Instance;
        T = NullLocalizer.Instance;

        _defaultDomain = _settingsRepository.Table.FirstOrDefault().DefaultDomain;

        _baseMembershipService = new Orchard.Users.Services.MembershipService(orchardServices, messageManager, userEventHandlers, clock, encryptionService);
    }

1 个答案:

答案 0 :(得分:4)

此行是问题的根源:

_defaultDomain = _settingsRepository.Table.FirstOrDefault().DefaultDomain;

不是在ctor中设置此值,而是将其包装在属性中(并添加null检查)。

除了简单的任务外,避免在ctor中加入任何逻辑。发生任何错误都可能会使您的网站崩溃。