示例#1
0
        private void Test(Action <ModelBuilder> buildModel, string expectedCode, Action <IModel> assert)
        {
            var modelBuilder = TestHelpers.Instance.CreateConventionBuilder();

            buildModel(modelBuilder);
            var model = modelBuilder.Model;

            var generator = new CSharpModelGenerator(new CSharpHelper());

            var builder = new IndentedStringBuilder();

            generator.Generate(model, builder);
            var code = builder.ToString();

            Assert.Equal(expectedCode, code);

            var build = new BuildSource
            {
                References =
                {
                    BuildReference.ByName("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),
                    BuildReference.ByName("System.Linq.Expressions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
                    BuildReference.ByName("System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
                    BuildReference.ByName("EntityFramework.Core"),
                    BuildReference.ByName("EntityFramework.Relational")
                },
                Sources = { @"
                    using System;
                    using Microsoft.Data.Entity;
                    using Microsoft.Data.Entity.Metadata;
                    using Microsoft.Data.Entity.Metadata.Conventions;
                    using Microsoft.Data.Entity.Migrations.Infrastructure;


                    public static class ModelSnapshot
                    {
                        public static IModel Model
                        {
                            get
                            {
                                var builder = new ModelBuilder(new ConventionSet());
                                " + code + @"

                                return builder.Model;
                            }
                        }
                   }
                " }
            };

            var assembly    = build.BuildInMemory();
            var factoryType = assembly.GetType("ModelSnapshot");
            var property    = factoryType.GetProperty("Model");
            var value       = (IModel)property.GetValue(null);

            Assert.NotNull(value);
            assert(value);
        }
        public override string GenerateMetadata(
            string migrationNamespace,
            Type contextType,
            string migrationName,
            string migrationId,
            IModel targetModel)
        {
            Check.NotEmpty(migrationNamespace, nameof(migrationNamespace));
            Check.NotNull(contextType, nameof(contextType));
            Check.NotEmpty(migrationName, nameof(migrationName));
            Check.NotEmpty(migrationId, nameof(migrationId));
            Check.NotNull(targetModel, nameof(targetModel));

            var builder    = new IndentedStringBuilder();
            var namespaces = new List <string>
            {
                "System",
                "Microsoft.Data.Entity",
                "Microsoft.Data.Entity.Infrastructure",
                "Microsoft.Data.Entity.Metadata",
                "Microsoft.Data.Entity.Migrations",
                contextType.Namespace
            };

            namespaces.AddRange(GetNamespaces(targetModel));
            foreach (var n in namespaces.Distinct())
            {
                builder
                .Append("using ")
                .Append(n)
                .AppendLine(";");
            }
            builder
            .AppendLine()
            .Append("namespace ").AppendLine(_code.Namespace(migrationNamespace))
            .AppendLine("{");
            using (builder.Indent())
            {
                builder
                .Append("[DbContext(typeof(").Append(_code.Reference(contextType)).AppendLine("))]")
                .Append("partial class ").AppendLine(_code.Identifier(migrationName))
                .AppendLine("{");
                using (builder.Indent())
                {
                    builder
                    .AppendLine("public override string Id")
                    .AppendLine("{");
                    using (builder.Indent())
                    {
                        builder.Append("get { return ").Append(_code.Literal(migrationId)).AppendLine("; }");
                    }
                    builder
                    .AppendLine("}")
                    .AppendLine()
                    .AppendLine("protected override void BuildTargetModel(ModelBuilder modelBuilder)")
                    .AppendLine("{");
                    using (builder.Indent())
                    {
                        // TODO: Optimize. This is repeated below
                        _modelGenerator.Generate("modelBuilder", targetModel, builder);
                    }
                    builder.AppendLine("}");
                }
                builder.AppendLine("}");
            }
            builder.AppendLine("}");

            return(builder.ToString());
        }
        private void Test(Action<ModelBuilder> buildModel, string expectedCode, Action<IModel> assert)
        {
            var modelBuilder = TestHelpers.Instance.CreateConventionBuilder();
            buildModel(modelBuilder);
            var model = modelBuilder.Model;

            var generator = new CSharpModelGenerator(new CSharpHelper());

            var builder = new IndentedStringBuilder();
            generator.Generate("builder", model, builder);
            var code = builder.ToString();

            Assert.Equal(expectedCode, code);

            var build = new BuildSource
            {
                References =
                {
#if !DNXCORE50
                    BuildReference.ByName("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),
                    BuildReference.ByName("System.Linq.Expressions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
                    BuildReference.ByName("System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
#endif
                    BuildReference.ByName("EntityFramework.Core"),
                    BuildReference.ByName("EntityFramework.Relational")
                },
                Sources = { @"
                    using Microsoft.Data.Entity;
                    using Microsoft.Data.Entity.Metadata;
                    using Microsoft.Data.Entity.Metadata.Conventions;

                    public static class ModelSnapshot
                    {
                        public static IModel Model
                        {
                            get
                            {
                                var builder = new ModelBuilder(new ConventionSet());
                                " + code + @"

                                return builder.Model;
                            }
                        }
                   }
                " }
            };

            var assembly = build.BuildInMemory();
            var factoryType = assembly.GetType("ModelSnapshot");
            var property = factoryType.GetTypeInfo().GetDeclaredProperty("Model");
            var value = (IModel)property.GetValue(null);

            Assert.NotNull(value);
            assert(value);
        }
        public override string GenerateMetadata(
            string migrationNamespace,
            Type contextType,
            string migrationName,
            string migrationId,
            string productVersion,
            IModel targetModel)
        {
            Check.NotEmpty(migrationNamespace, nameof(migrationNamespace));
            Check.NotNull(contextType, nameof(contextType));
            Check.NotEmpty(migrationName, nameof(migrationName));
            Check.NotEmpty(migrationId, nameof(migrationId));
            Check.NotEmpty(productVersion, nameof(productVersion));
            Check.NotNull(targetModel, nameof(targetModel));

            var builder = new IndentedStringBuilder();

            builder
            .AppendLine("using System;")
            .AppendLine("using Microsoft.Data.Entity;")
            .AppendLine("using Microsoft.Data.Entity.Metadata;")
            .AppendLine("using Microsoft.Data.Entity.Metadata.Builders;")
            .AppendLine("using Microsoft.Data.Entity.Relational.Migrations.Infrastructure;")
            .Append("using ").Append(contextType.Namespace).AppendLine(";")
            .AppendLine()
            .Append("namespace ").AppendLine(migrationNamespace)
            .AppendLine("{");
            using (builder.Indent())
            {
                builder
                .Append("[ContextType(typeof(").Append(_code.Reference(contextType)).AppendLine("))]")
                .Append("partial class ").AppendLine(_code.Identifier(migrationName))
                .AppendLine("{");
                using (builder.Indent())
                {
                    builder
                    .AppendLine("public override string Id")
                    .AppendLine("{");
                    using (builder.Indent())
                    {
                        builder.Append("get { return ").Append(_code.Literal(migrationId)).AppendLine("; }");
                    }
                    builder
                    .AppendLine("}")
                    .AppendLine()
                    .AppendLine("public override string ProductVersion")
                    .AppendLine("{");
                    using (builder.Indent())
                    {
                        builder.Append("get { return ").Append(_code.Literal(productVersion)).AppendLine("; }");
                    }
                    builder
                    .AppendLine("}")
                    .AppendLine()
                    .AppendLine("public override IModel Target")
                    .AppendLine("{");
                    using (builder.Indent())
                    {
                        builder
                        .AppendLine("get")
                        .AppendLine("{");
                        using (builder.Indent())
                        {
                            // TODO: Optimize. This is repeated below
                            _modelGenerator.Generate(targetModel, builder);
                        }
                        builder.AppendLine("}");
                    }
                    builder.AppendLine("}");
                }
                builder.AppendLine("}");
            }
            builder.AppendLine("}");

            return(builder.ToString());
        }