示例#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 CSharpSnapshotGenerator(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 System;
                    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);
        }
        protected void Test(Action <ModelBuilder> buildModel, string expectedCode, Action <IModel> assert)
        {
            var modelBuilder = CreateConventionalModelBuilder();

            modelBuilder.HasChangeTrackingStrategy(ChangeTrackingStrategy.Snapshot);
            buildModel(modelBuilder);

            modelBuilder.GetInfrastructure().Metadata.Validate();
            CreateModelValidator().Validate(modelBuilder.Model);
            var model = modelBuilder.Model;

            var generator = new CSharpSnapshotGenerator(new CSharpSnapshotGeneratorDependencies(new CSharpHelper()));

            var builder = new IndentedStringBuilder();

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

            Assert.Equal(expectedCode, code, ignoreLineEndingDifferences: true);

            var build = new BuildSource
            {
                Sources =
                {
                    @"
                    using System;
                    using Microsoft.EntityFrameworkCore;
                    using Microsoft.EntityFrameworkCore.Metadata;
                    using Microsoft.EntityFrameworkCore.Metadata.Conventions;
                    using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

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

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

            foreach (var buildReference in GetReferences())
            {
                build.References.Add(buildReference);
            }

            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);
        }