Beispiel #1
0
        [MigrationsTheory] // CodePlex 726
        public void Can_handle_max_length_set_to_MAX_in_SSDL()
        {
            var modelBuilder = new DbModelBuilder();

            modelBuilder.Entity <MigrationsCustomer>().Property(c => c.Photo).HasMaxLength(100);
            modelBuilder.Entity <MigrationsCustomer>().Property(c => c.FullName).HasMaxLength(100);
            var sourceModel = modelBuilder.Build(ProviderInfo).GetModel();

            modelBuilder.Entity <MigrationsCustomer>().Property(c => c.Photo).IsMaxLength();
            modelBuilder.Entity <MigrationsCustomer>().Property(c => c.FullName).IsMaxLength();
            var targetModel = modelBuilder.Build(ProviderInfo).GetModel();

            // Artificially add MaxLength=MAX to a couple of properties
            var customerEntity = targetModel
                                 .Elements().First()
                                 .Elements().First()
                                 .Elements().Single(e => e.Name.LocalName == "StorageModels")
                                 .Elements().Single(e => e.Name.LocalName == "Schema")
                                 .Elements().Single(e => e.Name.LocalName == "EntityType" && e.Attributes("Name").Any(a => a.Value == "MigrationsCustomer"));

            customerEntity.Elements().Single(e => e.Name.LocalName == "Property" && e.Attributes("Name").Any(a => a.Value == "FullName"))
            .Add(new XAttribute("MaxLength", "Max"));
            customerEntity.Elements().Single(e => e.Name.LocalName == "Property" && e.Attributes("Name").Any(a => a.Value == "Photo"))
            .Add(new XAttribute("MaxLength", "MAX"));

            DbProviderInfo providerInfo;
            var            storageMappingItemCollection = sourceModel.GetStorageMappingItemCollection(out providerInfo);

            var sourceMetadata = new EdmModelDiffer.ModelMetadata
            {
                Model = sourceModel,
                StoreItemCollection = storageMappingItemCollection.StoreItemCollection,
                StorageEntityContainerMapping
                    = storageMappingItemCollection.GetItems <StorageEntityContainerMapping>().Single(),
                ProviderManifest = GetProviderManifest(providerInfo),
                ProviderInfo     = providerInfo
            };

            var targetMetadata = new EdmModelDiffer.ModelMetadata
            {
                Model = targetModel,
                // Use the source model here since it doesn't effect the test and the SQL Server provider
                // won't load the target model
                StoreItemCollection = storageMappingItemCollection.StoreItemCollection,
                StorageEntityContainerMapping
                    = storageMappingItemCollection.GetItems <StorageEntityContainerMapping>().Single(),
                ProviderManifest = GetProviderManifest(providerInfo),
                ProviderInfo     = providerInfo
            };

            var operations = new EdmModelDiffer().Diff(sourceMetadata, targetMetadata, null, null);

            Assert.Equal(2, operations.Count());
            operations.OfType <AlterColumnOperation>().Each(
                o =>
            {
                Assert.Null(o.Column.MaxLength);
                Assert.Equal(100, ((AlterColumnOperation)o.Inverse).Column.MaxLength);
            });
        }
Beispiel #2
0
        [MigrationsTheory] // CodePlex 726
        public void Can_handle_max_length_set_to_MAX_in_SSDL()
        {
            var modelBuilder = new DbModelBuilder();
            modelBuilder.Entity<MigrationsCustomer>().Property(c => c.Photo).HasMaxLength(100);
            modelBuilder.Entity<MigrationsCustomer>().Property(c => c.FullName).HasMaxLength(100);
            var sourceModel = modelBuilder.Build(ProviderInfo).GetModel();

            modelBuilder.Entity<MigrationsCustomer>().Property(c => c.Photo).IsMaxLength();
            modelBuilder.Entity<MigrationsCustomer>().Property(c => c.FullName).IsMaxLength();
            var targetModel = modelBuilder.Build(ProviderInfo).GetModel();

            // Artificially add MaxLength=MAX to a couple of properties
            var customerEntity = targetModel
                .Elements().First()
                .Elements().First()
                .Elements().Single(e => e.Name.LocalName == "StorageModels")
                .Elements().Single(e => e.Name.LocalName == "Schema")
                .Elements().Single(e => e.Name.LocalName == "EntityType" && e.Attributes("Name").Any(a => a.Value == "MigrationsCustomer"));

            customerEntity.Elements().Single(e => e.Name.LocalName == "Property" && e.Attributes("Name").Any(a => a.Value == "FullName"))
                .Add(new XAttribute("MaxLength", "Max"));
            customerEntity.Elements().Single(e => e.Name.LocalName == "Property" && e.Attributes("Name").Any(a => a.Value == "Photo"))
                .Add(new XAttribute("MaxLength", "MAX"));

            DbProviderInfo providerInfo;
            var storageMappingItemCollection = sourceModel.GetStorageMappingItemCollection(out providerInfo);

            var sourceMetadata = new EdmModelDiffer.ModelMetadata
                                 {
                                     Model = sourceModel,
                                     StoreItemCollection = storageMappingItemCollection.StoreItemCollection,
                                     StorageEntityContainerMapping
                                         = storageMappingItemCollection.GetItems<StorageEntityContainerMapping>().Single(),
                                     ProviderManifest = GetProviderManifest(providerInfo),
                                     ProviderInfo = providerInfo
                                 };

            var targetMetadata = new EdmModelDiffer.ModelMetadata
                                 {
                                     Model = targetModel,
                                     // Use the source model here since it doesn't effect the test and the SQL Server provider
                                     // won't load the target model
                                     StoreItemCollection = storageMappingItemCollection.StoreItemCollection,
                                     StorageEntityContainerMapping
                                         = storageMappingItemCollection.GetItems<StorageEntityContainerMapping>().Single(),
                                     ProviderManifest = GetProviderManifest(providerInfo),
                                     ProviderInfo = providerInfo
                                 };

            var operations = new EdmModelDiffer().Diff(sourceMetadata, targetMetadata, null, null);

            Assert.Equal(2, operations.Count());
            operations.OfType<AlterColumnOperation>().Each(
                o =>
                {
                    Assert.Null(o.Column.MaxLength);
                    Assert.Equal(100, ((AlterColumnOperation)o.Inverse).Column.MaxLength);
                });
        }