C# 使用AutoFac选择基于实体的DbContext

C# 使用AutoFac选择基于实体的DbContext,c#,entity-framework,autofac,C#,Entity Framework,Autofac,我正在尝试根据autofac的实体指定要使用的dbcontext。以下是我的代码: Global.asax foreach (var database in DatabaseManager.Databases) { builder.Register<IDbContext>(c => new CodesObjectContext(database.ConnectionString, database.Alias)) //da

我正在尝试根据autofac的实体指定要使用的dbcontext。以下是我的代码:

Global.asax

foreach (var database in DatabaseManager.Databases)
            {
                builder.Register<IDbContext>(c => new CodesObjectContext(database.ConnectionString, database.Alias)) //database.Alias is database name
                    .As<IDbContext>()
                    .Named<IDbContext>(database.Alias)
                    .InstancePerLifetimeScope();
            }
builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
已更新 这是我的objectcontext类

public class CodesObjectContext : DbContext, IDbContext
    {
        public virtual string DatabaseName { get; private set; }
        public CodesObjectContext(string nameOrConnectionString, string databaseName)
            : base(nameOrConnectionString)
        {
            DatabaseName = databaseName;
        }            
}

那么,有没有一种方法可以匹配实体数据库属性和上下文数据库名称,然后只将此上下文传递给存储库构造函数?假设您已经定义了属性数据库名称,我将回答

//first in your base context, define contructor that takes string as database name.
//It should be something like this 
public MyContext(string mydbName = "DefaultDBName") : base(mydbName)
        {
        }

// In your getter, do something like this, get database Name from your attributes  
string dbName =
            (DatabaseName) Attribute.GetCustomAttribute(typeof(T), typeof (DatabaseName));
if(dbName==null) throw new Exception ("Missing Attribute");
else
{
// .Name might change depending on your attribute
string DatabaseName = db.Name;
// then init your context 
    using (var db = new MyContext(DatabaseName))
    {//do something }
}

我希望这会有帮助

嗨,曼塔斯,我的问题与JSONmaybe无关。你在找什么?嗨,芒泽,不。我想使用custom属性指定在repository中为上下文使用哪个数据库我已经更新了这个问题。我想你错过了autofac的范围我不确定什么是autofac,在阅读了更新之后,我仍然认为答案不需要更改,除非你还没有创建DatabaseName注释。
public class CodesObjectContext : DbContext, IDbContext
    {
        public virtual string DatabaseName { get; private set; }
        public CodesObjectContext(string nameOrConnectionString, string databaseName)
            : base(nameOrConnectionString)
        {
            DatabaseName = databaseName;
        }            
}
//first in your base context, define contructor that takes string as database name.
//It should be something like this 
public MyContext(string mydbName = "DefaultDBName") : base(mydbName)
        {
        }

// In your getter, do something like this, get database Name from your attributes  
string dbName =
            (DatabaseName) Attribute.GetCustomAttribute(typeof(T), typeof (DatabaseName));
if(dbName==null) throw new Exception ("Missing Attribute");
else
{
// .Name might change depending on your attribute
string DatabaseName = db.Name;
// then init your context 
    using (var db = new MyContext(DatabaseName))
    {//do something }
}