Exemple #1
0
        public void Throws_for_no_parameterless_constructor()
        {
            using (var directory = new TempDirectory())
            {
                var targetDir = directory.Path;
                var source    = new BuildSource
                {
                    TargetDir  = targetDir,
                    References =
                    {
                        BuildReference.ByName("System.Diagnostics.DiagnosticSource",                                             copyLocal: true),
                        BuildReference.ByName("System.Interactive.Async",                                                        copyLocal: true),
                        BuildReference.ByName("System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),
                        BuildReference.ByName("Microsoft.AspNetCore.Hosting.Abstractions",                                       copyLocal: true),
                        BuildReference.ByName("Microsoft.EntityFrameworkCore",                                                   copyLocal: true),
                        BuildReference.ByName("Microsoft.EntityFrameworkCore.Tools",                                             copyLocal: true),
                        BuildReference.ByName("Microsoft.EntityFrameworkCore.Design.Core",                                       copyLocal: true),
                        BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational",                                        copyLocal: true),
                        BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational.Design",                                 copyLocal: true),
                        BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer",                                         copyLocal: true),
                        BuildReference.ByName("Microsoft.Extensions.Caching.Abstractions",                                       copyLocal: true),
                        BuildReference.ByName("Microsoft.Extensions.Caching.Memory",                                             copyLocal: true),
                        BuildReference.ByName("Microsoft.Extensions.Configuration.Abstractions",                                 copyLocal: true),
                        BuildReference.ByName("Microsoft.Extensions.DependencyInjection",                                        copyLocal: true),
                        BuildReference.ByName("Microsoft.Extensions.DependencyInjection.Abstractions",                           copyLocal: true),
                        BuildReference.ByName("Microsoft.Extensions.FileProviders.Abstractions",                                 copyLocal: true),
                        BuildReference.ByName("Microsoft.Extensions.Logging",                                                    copyLocal: true),
                        BuildReference.ByName("Microsoft.Extensions.Logging.Abstractions",                                       copyLocal: true),
                        BuildReference.ByName("Microsoft.Extensions.Options",                                                    copyLocal: true),
                        BuildReference.ByName("Remotion.Linq",                                                                   copyLocal: true)
                    },
                    Sources = { @"
                            using Microsoft.EntityFrameworkCore;
                            using Microsoft.EntityFrameworkCore.Infrastructure;
                            using Microsoft.EntityFrameworkCore.Migrations;

                            namespace MyProject
                            {
                                internal class MyContext : DbContext
                                {
                                    public MyContext(DbContextOptions<MyContext> options) :base(options)  {}
                                }
                            }" }
                };
                var build = source.Build();
                using (var executor = new AppDomainOperationExecutor(targetDir, build.TargetName, targetDir, targetDir, "MyProject"))
                {
                    var ex = Assert.Throws <OperationException>(
                        () => executor.GetMigrations("MyContext"));

                    Assert.Equal(
                        DesignCoreStrings.NoParameterlessConstructor("MyContext"),
                        ex.Message);
                }
            }
        }
Exemple #2
0
        private IDictionary <Type, Func <DbContext> > FindContextTypes()
        {
            _logger.Value.LogDebug(DesignCoreStrings.LogFindingContexts);

            var contexts = new Dictionary <Type, Func <DbContext> >();

            // Look for IDbContextFactory implementations
            var contextFactories = _startupAssembly.GetConstructableTypes()
                                   .Where(t => typeof(IDbContextFactory <DbContext>).GetTypeInfo().IsAssignableFrom(t));

            foreach (var factory in contextFactories)
            {
                var manufacturedContexts =
                    from i in factory.ImplementedInterfaces
                    where i.GetTypeInfo().IsGenericType &&
                    i.GetGenericTypeDefinition() == typeof(IDbContextFactory <>)
                    select i.GenericTypeArguments[0];
                foreach (var context in manufacturedContexts)
                {
                    contexts.Add(
                        context,
                        () => ((IDbContextFactory <DbContext>)Activator.CreateInstance(factory.AsType())).Create(
                            CreateFactoryOptions()));
                }
            }

            // Look for DbContext classes registered in the service provider
            var registeredContexts = _runtimeServices.GetServices <DbContextOptions>()
                                     .Select(o => o.ContextType);

            foreach (var context in registeredContexts.Where(c => !contexts.ContainsKey(c)))
            {
                contexts.Add(
                    context,
                    FindContextFactory(context) ?? (() => (DbContext)_runtimeServices.GetRequiredService(context)));
            }

            // Look for DbContext classes in assemblies
            var types = _startupAssembly.GetConstructableTypes()
                        .Concat(_assembly.GetConstructableTypes())
                        .Select(i => i.AsType())
                        .ToList();
            var contextTypes = types.Where(t => typeof(DbContext).IsAssignableFrom(t))
                               .Concat(
                types.Where(t => typeof(Migration).IsAssignableFrom(t))
                .Select(t => t.GetTypeInfo().GetCustomAttribute <DbContextAttribute>()?.ContextType)
                .Where(t => t != null))
                               .Distinct();

            foreach (var context in contextTypes.Where(c => !contexts.ContainsKey(c)))
            {
                contexts.Add(
                    context,
                    FindContextFactory(context) ?? (Func <DbContext>)(() =>
                {
                    try
                    {
                        return((DbContext)Activator.CreateInstance(context));
                    }
                    catch (MissingMethodException ex)
                    {
                        throw new OperationException(DesignCoreStrings.NoParameterlessConstructor(context.Name), ex);
                    }
                }));
            }

            return(contexts);
        }