public void Model_differ_breaks_foreign_key_cycles()
        {
            var model = new Entity.Metadata.Model();

            var firstType = model.AddEntityType("First");
            var firstKey = firstType.SetPrimaryKey(firstType.AddProperty("ID", typeof(int), true));
            var firstFk = firstType.AddProperty("FK", typeof(int), true);

            var secondType = model.AddEntityType("Second");
            var secondKey = secondType.SetPrimaryKey(secondType.AddProperty("ID", typeof(int), true));
            var secondFk = secondType.AddProperty("FK", typeof(int), true);

            firstType.AddForeignKey(firstFk, secondKey);
            secondType.AddForeignKey(secondFk, firstKey);

            var modelDiffer = new ModelDiffer(new RelationalTypeMapper());

            var result = modelDiffer.GetDifferences(null, model);

            Assert.Equal(3, result.Count);

            var firstOperation = result[0] as CreateTableOperation;
            var secondOperation = result[1] as CreateTableOperation;
            var thirdOperation = result[2] as AddForeignKeyOperation;

            Assert.NotNull(firstOperation);
            Assert.NotNull(secondOperation);
            Assert.NotNull(thirdOperation);

            Assert.Equal(0, firstOperation.ForeignKeys.Count);
            Assert.Equal(1, secondOperation.ForeignKeys.Count);
            Assert.Equal(firstOperation.Name, thirdOperation.Table);
        }
Exemple #2
0
        public void Can_set_model()
        {
            var sequence = new Sequence("Foo");

            var model = new Entity.Metadata.Model();

            sequence.Model = model;
            Assert.Same(model, sequence.Model);
        }
        protected Entity.Metadata.Model CreateModel()
        {
            var model        = new Entity.Metadata.Model();
            var modelBuilder = new BasicModelBuilder(model);

            // TODO: Uncomment when complex types are supported
            //builder.ComplexType<Location>();
            modelBuilder.Entity <Customer>(ps =>
            {
                ps.Property(c => c.Name);
                ps.Key(c => c.Id);
                ps.ForRelational().Table("Customers");
            });

            return(model);
        }
Exemple #4
0
        private static IModel BuildModel(bool generateKeyValues, bool computeNonKeyValue)
        {
            var model      = new Entity.Metadata.Model();
            var entityType = model.AddEntityType(typeof(T1));

            var key = entityType.GetOrAddProperty("Id", typeof(int));

            key.ValueGenerated = generateKeyValues ? ValueGenerated.OnAdd : ValueGenerated.Never;
            key.Relational().Column = "Col1";
            entityType.GetOrSetPrimaryKey(key);

            var nonKey = entityType.GetOrAddProperty("Name", typeof(string));

            nonKey.IsConcurrencyToken = computeNonKeyValue;

            nonKey.Relational().Column = "Col2";
            nonKey.ValueGenerated = computeNonKeyValue ? ValueGenerated.OnAddOrUpdate : ValueGenerated.Never;

            return(model);
        }
        private static IModel BuildModel(bool generateKeyValues, bool computeNonKeyValue)
        {
            var model = new Entity.Metadata.Model();

            var entityType = model.AddEntityType(typeof(T1));

            var key = entityType.AddProperty("Id", typeof(int));
            key.IsShadowProperty = false;
            key.ValueGenerated = generateKeyValues ? ValueGenerated.OnAdd : ValueGenerated.Never;
            key.Relational().ColumnName = "Col1";
            entityType.GetOrSetPrimaryKey(key);

            var nonKey = entityType.AddProperty("Name", typeof(string));
            nonKey.IsShadowProperty = false;
            nonKey.Relational().ColumnName = "Col2";
            nonKey.ValueGenerated = computeNonKeyValue ? ValueGenerated.OnAddOrUpdate : ValueGenerated.Never;

            return model;
        }
        public void Compare_returns_0_only_for_commands_that_are_equal()
        {
            var model = new Entity.Metadata.Model();
            var entityType = model.AddEntityType(typeof(object));

            var optionsBuilder = new DbContextOptionsBuilder()
                .UseModel(model);
            optionsBuilder.UseInMemoryStore(persist: false);

            var contextServices = ((IAccessor<IServiceProvider>)new DbContext(optionsBuilder.Options)).Service;
            var stateManager = contextServices.GetRequiredService<IStateManager>();

            var key = entityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true);
            entityType.GetOrSetPrimaryKey(key);

            var entry1 = stateManager.GetOrCreateEntry(new object());
            entry1[key] = 1;
            entry1.SetEntityState(EntityState.Added);
            var modificationCommandAdded = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource());
            modificationCommandAdded.AddEntry(entry1);

            var entry2 = stateManager.GetOrCreateEntry(new object());
            entry2[key] = 2;
            entry2.SetEntityState(EntityState.Modified);
            var modificationCommandModified = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource());
            modificationCommandModified.AddEntry(entry2);

            var entry3 = stateManager.GetOrCreateEntry(new object());
            entry3[key] = 3;
            entry3.SetEntityState(EntityState.Deleted);
            var modificationCommandDeleted = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource());
            modificationCommandDeleted.AddEntry(entry3);

            var mCC = new ModificationCommandComparer();

            Assert.True(0 == mCC.Compare(modificationCommandAdded, modificationCommandAdded));
            Assert.True(0 == mCC.Compare(null, null));
            Assert.True(0 == mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource())));

            Assert.True(0 > mCC.Compare(null, new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource())));
            Assert.True(0 < mCC.Compare(new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource()), null));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource()),
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource())));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource()),
                new ModificationCommand("A", "foo", new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("A", "foo", new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource())));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource()),
                new ModificationCommand("B", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("B", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource()),
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource())));

            Assert.True(0 > mCC.Compare(modificationCommandModified, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandModified));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandDeleted));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandModified));
            Assert.True(0 < mCC.Compare(modificationCommandModified, modificationCommandDeleted));
        }
        private static IModel CreateCyclicFKModel()
        {
            var model = new Entity.Metadata.Model();
            var modelBuilder = new BasicModelBuilder(model);

            modelBuilder.Entity<FakeEntity>(b =>
                {
                    b.Key(c => c.Id);
                    b.Property(c => c.Value);
                });

            modelBuilder.Entity<RelatedFakeEntity>(b =>
                {
                    b.Key(c => c.Id);
                    b.ForeignKey<FakeEntity>(c => c.RelatedId);
                });

            modelBuilder
                .Entity<FakeEntity>()
                .ForeignKey<RelatedFakeEntity>(c => c.RelatedId);

            return model;
        }
        //TODO: investigate doing this as a builder pattern
        public virtual IModel ConstructCodeGenModel(
            [NotNull] IModel relationalModel, [NotNull] MetadataModelNameMapper nameMapper)
        {
            Check.NotNull(relationalModel, nameof(relationalModel));
            Check.NotNull(nameMapper, nameof(nameMapper));

            var codeGenModel = new Entity.Metadata.Model();
            foreach (var relationalEntityType in relationalModel.EntityTypes.Cast<EntityType>())
            {
                var codeGenEntityType = codeGenModel
                    .AddEntityType(nameMapper.EntityTypeToClassNameMap[relationalEntityType]);
                _relationalToCodeGenEntityTypeMap[relationalEntityType] = codeGenEntityType;
                codeGenEntityType.Relational().TableName = relationalEntityType.Relational().TableName;
                codeGenEntityType.Relational().Schema = relationalEntityType.Relational().Schema;
                var errorMessage = relationalEntityType[AnnotationNameEntityTypeError];
                if (errorMessage != null)
                {
                    codeGenEntityType.AddAnnotation(AnnotationNameEntityTypeError, 
                        Strings.UnableToGenerateEntityType(codeGenEntityType.Name, errorMessage));
                }

                foreach (var relationalProperty in relationalEntityType.Properties)
                {
                    var codeGenProperty = codeGenEntityType.AddProperty(
                        nameMapper.PropertyToPropertyNameMap[relationalProperty],
                        ((IProperty)relationalProperty).ClrType);
                    _relationalToCodeGenPropertyMap[relationalProperty] = codeGenProperty;
                    CopyPropertyFacets(relationalProperty, codeGenProperty);
                }


                var primaryKey = relationalEntityType.FindPrimaryKey();
                if (primaryKey != null)
                {
                    codeGenEntityType.SetPrimaryKey(
                        primaryKey.Properties
                            .Select(p => _relationalToCodeGenPropertyMap[p])
                            .ToList());
                }
            } // end of loop over all relational EntityTypes

            AddForeignKeysToCodeGenModel(relationalModel, codeGenModel);

            return codeGenModel;
        }
        private static IModel BuildModel(bool generateKeyValues, bool computeNonKeyValue)
        {
            var model = new Entity.Metadata.Model();

            var entityType = model.AddEntityType(typeof(T1));

            var key = entityType.GetOrAddProperty("Id", typeof(int));
            key.StoreGeneratedPattern = generateKeyValues ? StoreGeneratedPattern.Identity : StoreGeneratedPattern.None;
            key.GenerateValueOnAdd = generateKeyValues;
            key.Relational().Column = "Col1";
            entityType.GetOrSetPrimaryKey(key);

            var nonKey = entityType.GetOrAddProperty("Name", typeof(string));
            nonKey.Relational().Column = "Col2";
            nonKey.StoreGeneratedPattern = computeNonKeyValue ? StoreGeneratedPattern.Computed : StoreGeneratedPattern.None;

            return model;
        }