내가 간단한 DbContext
이처럼 보이는이 범위 및 apperently container.RegisterFuncFactory<DbContext, MyDbContext>(Lifestyle.Scoped);
DbContext 종속성
public static void RegisterFuncFactory<TService, TImpl>(
this Container container, Lifestyle lifestyle = null)
where TService : class
where TImpl : class, TService
{
lifestyle = lifestyle ?? Lifestyle.Transient;
var producer = lifestyle.CreateProducer<TService, TImpl>(container);
container.RegisterSingleton<Func<TService>>(producer.GetInstance);
}
그러나이 같은 간단한 경우는 불가능합니다 이 메시지 때문에 DbContext
은
대상 컨텍스트 'MyDbContext'를 구성 할 수 없습니다. 기본 생성자를 추가하거나 IDbContextFactory 구현을 제공하십시오.
은 내가 IDbContextFactory
의 생각처럼 정말 해달라고, 그래서 유일한 해결책은 나는 MyDbContext
에 종속성을 제거 속성으로 설정하십시오 RegisterFuncFactory
방법을 수정하고 수동으로 컨텍스트를 초기화하는 것입니다 가지고 올 수 :
internal static void RegisterFuncFactory<TService, TImpl>(this Container container, Func<TImpl> instanceProducer, Lifestyle lifestyle = null) where TService : class where TImpl : class, TService
{
lifestyle = lifestyle ?? Lifestyle.Transient;
var producer = lifestyle.CreateProducer<TService>(instanceProducer, container);
container.Register<Func<TService>>(() => producer.GetInstance, Lifestyle.Singleton);
}
container.RegisterFuncFactory<DbContext, MyDbContext>(() => new MyDbContext
{
UserContext = container.GetInstance<IUserContext>()
}, Lifestyle.Scoped);
우아한 것은 아니지만, 내가 원하는 것을 수행하는 "더 좋은"방법이 있습니까? 나는 문맥에 대한 의존성을 명시 적으로 좋아하지만 가능하지는 않다.
는 오류가에서 오는
UPDATE :
'System.Data.Entity.Migrations.Infrastructure.MigrationsException' 는 EntityFramework.dll에서 발생했지만 사용자 코드
에서 처리되지 않은
이 코드에서 Query 메서드의 return 문은 다음과 같습니다.
internal sealed class EntityFrameworkRepository<TEntity> : IEntityWriter<TEntity>, IEntityReader<TEntity> where TEntity : Entity
{
private readonly Func<DbContext> _contextProvider;
public EntityFrameworkRepository(Func<DbContext> contextProvider)
{
_contextProvider = contextProvider;
}
public IQueryable<TEntity> Query()
{
var context = _contextProvider();
return context.Set<TEntity>().AsNoTracking();
}
// Methods removed for brevity
}
:
이
는 다음과 같은 구성 루트에 연결되어? EF 마이그레이션? – Steven예, EntityFramework에서''System.Data.Entity.Migrations.Infrastructure.MigrationsException '오류가 발생했습니다.dll하지만 사용자 코드에서 처리되지 않았습니다'반환 statemenet 동안 여기 :'var context = _contextProvider(); return context.Set() .AsNoTracking();' –
janhartmann
@janhartmann'Entity' (제약 조건)이란 무엇입니까? 나는 당신이'class' 제약 조건을 지정해야한다고 생각한다. 왜냐하면 지금은'TEntity'가 허용되지 않는 인터페이스가 될 수 있기 때문이다. –