c# - 使用自定义构造函数进行代码优先迁移

标签 c# asp.net-mvc structuremap

我正在将代码优先迁移与 Entity Framework 结合使用。我正在使用带有连接字符串的自定义构造函数,该连接字符串特定于登录到应用程序的特定用户。

当加载应用程序时,我希望使用用户连接字符串初始化数据库,并在数据库上下文发生任何更改时将其迁移到最新版本。我正在使用结构图来注入(inject)工作单元。

全局阿萨克斯

ObjectFactory.Configure(cfg =>
        {
            cfg.AddRegistry(new StandardRegistry());
            cfg.AddRegistry(new ControllerRegistry());
            cfg.AddRegistry(new ActionFilterRegistry(() => Container ?? ObjectFactory.Container));
            cfg.AddRegistry(new MvcRegistry());
            cfg.AddRegistry(new TaskRegistry());
            cfg.AddRegistry(new ModelMetadataRegistry());
        });

StandardRegistry 类

 For<ICustomerUnitOfWork>().Use(new CustomerUnitOfWork(new CustomerContext()));
        For<CustomerContext>().Use(new CustomerContext());

此时我不知道用户的connectionstring,因为用户没有登录。问题是model改变了,structuremap抛出异常,因为context model改变了。

上下文

private static string _connectionString;

    static CustomerContext()
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<CustomerContext, Configuration>());
    }

    public CustomerContext()
        : base(string.IsNullOrEmpty(_connectionString) ? "FooDatabase" : _connectionString)
    {
        if (string.IsNullOrEmpty(_connectionString))
            Database.SetInitializer(new CustomerContextInitializer());
    }


    public CustomerContext(string connectionString)
        : base(connectionString)
    {
        if (string.IsNullOrEmpty(_connectionString))
            Database.SetInitializer(new CustomerContextInitializer());
        _connectionString = connectionString;
    }

CustomerContextInitializer

 public class CustomerContextInitializer : CreateDatabaseIfNotExists<CustomerContext>
{
    protected override void Seed(CustomerContext context)
    {
        var informationService = new InformationService
        {
            ObjectState = ObjectState.Added,
            ConnectorClass =
                "Conexio.InformationService.Contracts.IInformationService, Conexio.InformationService.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
            DisplayName = "IInformationService"
        };

有没有办法在运行时将规则添加到 standardRegistry 类以使用连接字符串调用构造函数?或者有其他方法可以解决这个问题吗?

最佳答案

如果我没有正确理解您的问题,您最好的解决方法是创建一个工厂类,负责在运行时创建您的 ICustomerUnitOfWork 实例,然后可以在您的 StructureMap 注册表中进行配置像这样:

this.For<ICustomerUnitOfWork>().Use(ctx => ctx.GetInstance<YourFactoryClass>().ResolveCustomerUnitOfWork());

然后,您的 ICustomerUnitOfWork 将解析为您的工厂类,该工厂类可以将自己的一组依赖项注入(inject)其中,以确定正确的上下文是什么。

public interface ICustomerUnitOfWork
{

}

public class YourFactoryClass
{
    public YourFactoryClass(string connectionString)
    {

    }

    public ICustomerUnitOfWork ResolveCustomerUnitOfWork()
    {
        // Perform runtime configuration

        return unitOfWork;
    }
}

希望对您有所帮助!

关于c# - 使用自定义构造函数进行代码优先迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30477066/

相关文章:

c# - WebRequest 和协议(protocol)无关的 URL

c# - DateTime.AddYears 在闰年的行为

c# - 从 Angular JS 调用 Web API 错误 : The 'Access-Control-Allow-Origin' header contains multiple values, Origin is not allowed access

asp.net-mvc-4 - 处理注入(inject)的 HttpClient

c# - UWP:如何将参数传递给导航页面的ViewModel?

c# - 如何调用接受两个模型的 Controller 方法?

asp.net-mvc - 使用 ELMAH 在 EventLog 中记录错误

asp.net-mvc - 有没有一种方法可以在不使用 View 模型的情况下从 ActionResult 方法传递变量?

c# - 在 C# 中具有我自己的属性的 StructureMap

assemblies - StructureMap 和扫描程序集