public string Write(ScaffoldedMigration scaffoldedMigration, bool rescaffolding = false, bool force = false, string name = null)
        {
            Contract.Requires(scaffoldedMigration != null);

            var userCodeFileName = scaffoldedMigration.MigrationId + "." + scaffoldedMigration.Language;
            var userCodePath = Path.Combine(scaffoldedMigration.Directory, userCodeFileName);
            var designerCodeFileName = scaffoldedMigration.MigrationId + ".Designer."
                                       + scaffoldedMigration.Language;
            var designerCodePath = Path.Combine(scaffoldedMigration.Directory, designerCodeFileName);
            var resourcesFileName = scaffoldedMigration.MigrationId + ".resx";
            var resourcesPath = Path.Combine(scaffoldedMigration.Directory, resourcesFileName);

            if (rescaffolding && !force)
            {
                var absoluteUserCodePath = Path.Combine(_command.Project.GetProjectDir(), userCodePath);

                if (!string.Equals(scaffoldedMigration.UserCode, File.ReadAllText(absoluteUserCodePath)))
                {
                    Contract.Assert(!string.IsNullOrWhiteSpace(name));

                    _command.WriteWarning(Strings.RescaffoldNoForce(name));
                }
            }
            else
            {
                _command.Project.AddFile(userCodePath, scaffoldedMigration.UserCode);
            }

            WriteResources(userCodePath, resourcesPath, scaffoldedMigration.Resources);
            _command.Project.AddFile(designerCodePath, scaffoldedMigration.DesignerCode);

            return userCodePath;
        }
        /// <inheritdoc />
        public override ScaffoldedMigration Generate(
            string migrationId,
            IEnumerable<MigrationOperation> operations,
            string sourceModel,
            string targetModel,
            string @namespace,
            string className)
        {
            Check.NotEmpty(migrationId, "migrationId");
            Check.NotNull(operations, "operations");
            Check.NotEmpty(targetModel, "targetModel");
            Check.NotEmpty(className, "className");

            className = ScrubName(className);

            _newTableForeignKeys
                = (from ct in operations.OfType<CreateTableOperation>()
                   from cfk in operations.OfType<AddForeignKeyOperation>()
                   where ct.Name.EqualsIgnoreCase(cfk.DependentTable)
                   select Tuple.Create(ct, cfk)).ToList();

            _newTableIndexes
                = (from ct in operations.OfType<CreateTableOperation>()
                   from cfk in operations.OfType<CreateIndexOperation>()
                   where ct.Name.EqualsIgnoreCase(cfk.Table)
                   select Tuple.Create(ct, cfk)).ToList();

            var generatedMigration
                = new ScaffoldedMigration
                      {
                          MigrationId = migrationId,
                          Language = "vb",
                          UserCode = Generate(operations, @namespace, className),
                          DesignerCode = Generate(migrationId, sourceModel, targetModel, @namespace, className)
                      };

            if (!string.IsNullOrWhiteSpace(sourceModel))
            {
                generatedMigration.Resources.Add("Source", sourceModel);
            }

            generatedMigration.Resources.Add("Target", targetModel);

            return generatedMigration;
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public override ScaffoldedMigration Generate(
            string migrationId,
            IEnumerable <MigrationOperation> operations,
            string sourceModel,
            string targetModel,
            string @namespace,
            string className)
        {
            Check.NotEmpty(migrationId, "migrationId");
            Check.NotNull(operations, "operations");
            Check.NotEmpty(targetModel, "targetModel");
            Check.NotEmpty(className, "className");

            className = ScrubName(className);

            _newTableForeignKeys
                = (from ct in operations.OfType <CreateTableOperation>()
                   from cfk in operations.OfType <AddForeignKeyOperation>()
                   where ct.Name.EqualsIgnoreCase(cfk.DependentTable)
                   select Tuple.Create(ct, cfk)).ToList();

            _newTableIndexes
                = (from ct in operations.OfType <CreateTableOperation>()
                   from cfk in operations.OfType <CreateIndexOperation>()
                   where ct.Name.EqualsIgnoreCase(cfk.Table)
                   select Tuple.Create(ct, cfk)).ToList();

            var generatedMigration
                = new ScaffoldedMigration
                {
                MigrationId  = migrationId,
                Language     = "vb",
                UserCode     = Generate(operations, @namespace, className),
                DesignerCode = Generate(migrationId, sourceModel, targetModel, @namespace, className)
                };

            if (!string.IsNullOrWhiteSpace(sourceModel))
            {
                generatedMigration.Resources.Add("Source", sourceModel);
            }

            generatedMigration.Resources.Add("Target", targetModel);

            return(generatedMigration);
        }
        private void TestWrite(
            Func<System.Data.Entity.Migrations.Utilities.MigrationWriter, ScaffoldedMigration, string> action,
            bool skipUserCodeVerification = false)
        {
            var command = CreateCommand(_projectDir);
            var writer = new System.Data.Entity.Migrations.Utilities.MigrationWriter(command);
            var scaffoldedMigration = new ScaffoldedMigration
                                          {
                                              MigrationId = MigrationId,
                                              Language = Language,
                                              Directory = MigrationsDirectory,
                                              UserCode = "The user code.",
                                              DesignerCode = "The designer code.",
                                              Resources =
                                                  {
                                                      { ResourceName, "The resource." }
                                                  }
                                          };

            var relativeUserCodePath = action(writer, scaffoldedMigration);

            Assert.Equal(UserCodePath, relativeUserCodePath);

            if (!skipUserCodeVerification)
            {
                var userCodePath = Path.Combine(_projectDir, UserCodePath);
                Assert.Equal("The user code.", File.ReadAllText(userCodePath));
            }

            var designerCodePath = Path.Combine(_projectDir, DesignerCodePath);
            Assert.Equal("The designer code.", File.ReadAllText(designerCodePath));

            var resourcesPath = Path.Combine(_projectDir, ResourcesPath);

            using (var reader = new ResXResourceReader(resourcesPath))
            {
                var resources = reader.Cast<DictionaryEntry>();

                Assert.Equal(1, resources.Count());
                Assert.Contains(new DictionaryEntry(ResourceName, "The resource."), resources);
            }
        }
 protected virtual string WriteMigration(string name, bool force, ScaffoldedMigration scaffoldedMigration, bool rescaffolding)
 {
     return new MigrationWriter(this).Write(scaffoldedMigration, rescaffolding, force, name);
 }
 protected override string WriteMigration(string name, bool force, ScaffoldedMigration scaffoldedMigration, bool rescaffolding)
 {
     return name;
 }