public void Constructor_throws_when_referenced_key_not_on_referenced_entity()
        {
            var model = new Model();

            var principalEntityType = model.AddEntityType("R");
            var dependentEntityType = model.AddEntityType("D");
            var fk = dependentEntityType.AddProperty("Fk", typeof(int), shadowProperty: true);

            var principalKey = dependentEntityType.SetPrimaryKey(fk);

            Assert.Throws<ArgumentException>(() =>
                new ForeignKey(new[] { fk }, principalKey, principalEntityType));
        }
        public void Constructor_throws_when_referenced_key_not_on_referenced_entity()
        {
            var model = new Model();

            var principalEntityType = model.AddEntityType("R");
            var dependentEntityType = model.AddEntityType("D");
            var fk = dependentEntityType.AddProperty("Fk", typeof(int));

            var principalKey = dependentEntityType.SetPrimaryKey(fk);

            Assert.Equal(
                CoreStrings.ForeignKeyReferencedEntityKeyMismatch("{'Fk'}", "R"),
                Assert.Throws<ArgumentException>(() => new ForeignKey(new[] { fk }, principalKey, dependentEntityType, principalEntityType)).Message);
        }
        public virtual void Detects_a_null_primary_key()
        {
            var model = new Model();
            model.AddEntityType(typeof(A));

            VerifyError(CoreStrings.EntityRequiresKey(typeof(A).FullName), model);
        }
        public virtual void Detects_shadow_entities()
        {
            var model = new Model();
            model.AddEntityType("A");

            VerifyError(CoreStrings.ShadowEntity("A"), model);
        }
        public virtual void Passes_on_escapable_foreign_key_cycles_not_starting_at_hub()
        {
            var model = new Model();
            var entityA = model.AddEntityType(typeof(A));
            SetPrimaryKey(entityA);
            var keyA1 = CreateKey(entityA);
            var keyA2 = CreateKey(entityA, startingPropertyIndex: 1, propertyCount: 2);
            var entityB = model.AddEntityType(typeof(B));
            SetPrimaryKey(entityB);
            var keyB1 = CreateKey(entityB);
            var keyB2 = CreateKey(entityB, startingPropertyIndex: 0, propertyCount: 2);

            CreateForeignKey(keyA1, keyB1);
            CreateForeignKey(keyB1, keyA1);
            CreateForeignKey(keyB2, keyA2);

            Validate(model);
        }
        private static Model CreateModel(bool generateValues = true)
        {
            var model = new Model();

            var entityType = model.AddEntityType("Led");
            entityType.AddProperty("Zeppelin", typeof(Guid));
            entityType.AddProperty("Stairway", typeof(Guid)).RequiresValueGenerator = generateValues;

            return model;
        }
        public virtual void Detects_shadow_keys()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(A));
            SetPrimaryKey(entityType);
            var keyProperty = entityType.AddProperty("Key", typeof(int));
            entityType.AddKey(keyProperty);

            VerifyWarning(CoreStrings.ShadowKey("{'Key'}", typeof(A).FullName, "{'Key'}"), model);
        }
        public void Creates_shadow_state_only_entry_when_entity_is_fully_shadow_state()
        {
            var model = new Model();
            var entityType = model.AddEntityType("RedHook");
            entityType.AddProperty("Long", typeof(int));
            entityType.AddProperty("Hammer", typeof(string));

            var contextServices = TestHelpers.Instance.CreateContextServices(model);
            var stateManager = contextServices.GetRequiredService<IStateManager>();
            var factory = contextServices.GetRequiredService<IInternalEntityEntryFactory>();

            var entry = factory.Create(stateManager, entityType, new Random());

            Assert.IsType<InternalShadowEntityEntry>(entry);

            Assert.Same(stateManager, entry.StateManager);
            Assert.Same(entityType, entry.EntityType);
            Assert.Null(entry.Entity);
        }
        public void Creates_mixed_entry_when_entity_CLR_entity_type_and_shadow_properties()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(RedHook));
            var property1 = entityType.AddProperty("Long", typeof(int));
            property1.IsShadowProperty = false;
            entityType.AddProperty("Hammer", typeof(string));

            var contextServices = TestHelpers.Instance.CreateContextServices(model);
            var stateManager = contextServices.GetRequiredService<IStateManager>();
            var factory = contextServices.GetRequiredService<IInternalEntityEntryFactory>();

            var entity = new RedHook();
            var entry = factory.Create(stateManager, entityType, entity);

            Assert.IsType<InternalMixedEntityEntry>(entry);

            Assert.Same(stateManager, entry.StateManager);
            Assert.Same(entityType, entry.EntityType);
            Assert.Same(entity, entry.Entity);
        }
        protected override Model BuildModel()
        {
            var model = new Model();

            var someSimpleEntityType = model.AddEntityType(typeof(SomeSimpleEntityBase).FullName);
            var simpleKeyProperty = someSimpleEntityType.AddProperty("Id", typeof(int));
            simpleKeyProperty.RequiresValueGenerator = true;
            someSimpleEntityType.GetOrSetPrimaryKey(simpleKeyProperty);

            var someCompositeEntityType = model.AddEntityType(typeof(SomeCompositeEntityBase).FullName);
            var compositeKeyProperty1 = someCompositeEntityType.AddProperty("Id1", typeof(int));
            var compositeKeyProperty2 = someCompositeEntityType.AddProperty("Id2", typeof(string));
            someCompositeEntityType.GetOrSetPrimaryKey(new[] { compositeKeyProperty1, compositeKeyProperty2 });

            var entityType1 = model.AddEntityType(typeof(SomeEntity).FullName);
            entityType1.BaseType = someSimpleEntityType;
            var property3 = entityType1.AddProperty("Name", typeof(string));
            property3.IsConcurrencyToken = true;

            var entityType2 = model.AddEntityType(typeof(SomeDependentEntity).FullName);
            entityType2.BaseType = someCompositeEntityType;
            var fk = entityType2.AddProperty("SomeEntityId", typeof(int));
            entityType2.GetOrAddForeignKey(new[] { fk }, entityType1.FindPrimaryKey(), entityType1);
            var justAProperty = entityType2.AddProperty("JustAProperty", typeof(int));
            justAProperty.RequiresValueGenerator = true;

            var entityType3 = model.AddEntityType(typeof(FullNotificationEntity));
            entityType3.GetOrSetPrimaryKey(entityType3.AddProperty("Id", typeof(int)));
            var property6 = entityType3.AddProperty("Name", typeof(string));
            property6.IsConcurrencyToken = true;

            var entityType4 = model.AddEntityType(typeof(ChangedOnlyEntity));
            entityType4.GetOrSetPrimaryKey(entityType4.AddProperty("Id", typeof(int)));
            var property8 = entityType4.AddProperty("Name", typeof(string));
            property8.IsConcurrencyToken = true;

            var entityType5 = model.AddEntityType(typeof(SomeMoreDependentEntity).FullName);
            entityType5.BaseType = someSimpleEntityType;
            var fk5a = entityType5.AddProperty("Fk1", typeof(int));
            var fk5b = entityType5.AddProperty("Fk2", typeof(string));
            entityType5.GetOrAddForeignKey(new[] { fk5a, fk5b }, entityType2.FindPrimaryKey(), entityType2);

            return model;
        }
        public void Context_will_use_explicit_model_if_set_in_config()
        {
            var model = new Model();
            model.AddEntityType(typeof(TheGu));

            using (var context = new EarlyLearningCenter(
                TestHelpers.Instance.CreateServiceProvider(),
                new DbContextOptionsBuilder().UseModel(model).Options))
            {
                Assert.Equal(
                    new[] { typeof(TheGu).FullName },
                    context.Model.GetEntityTypes().Select(e => e.Name).ToArray());
            }
        }
        public void Can_ignore_existing_entity_type_using_entity_type_name_explicitly()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(Customer).FullName);
            var modelBuilder = CreateModelBuilder(model);

            Assert.Same(entityType, modelBuilder.Entity(typeof(Customer).FullName, ConfigurationSource.Convention).Metadata);
            Assert.False(modelBuilder.Ignore(typeof(Customer).FullName, ConfigurationSource.DataAnnotation));
            Assert.NotNull(model.FindEntityType(typeof(Customer).FullName));

            Assert.True(modelBuilder.Ignore(typeof(Customer).FullName, ConfigurationSource.Explicit));
            Assert.Null(model.FindEntityType(typeof(Customer).FullName));

            Assert.Equal(Strings.EntityIgnoredExplicitly(typeof(Customer).FullName),
                Assert.Throws<InvalidOperationException>(() =>
                    modelBuilder.Entity(typeof(Customer).FullName, ConfigurationSource.Explicit)).Message);
        }
        private ForeignKey CreateOneToManySameHierarchyFK()
        {
            var model = new Model();

            var baseEntityType = model.AddEntityType(typeof(NavigationBase));
            var property1 = baseEntityType.AddProperty(NavigationBase.IdProperty);
            var pk = baseEntityType.GetOrSetPrimaryKey(property1);

            var dependentEntityType = model.AddEntityType(typeof(OneToManyDependent));
            dependentEntityType.BaseType = baseEntityType;
            var fkProp = dependentEntityType.AddProperty("Fk", typeof(int));
            var fk = dependentEntityType.AddForeignKey(new[] { fkProp }, pk, baseEntityType);

            baseEntityType.AddNavigation("OneToManyDependents", fk, pointsToPrincipal: false);
            return fk;
        }
        private ForeignKey CreateOneToManyFK()
        {
            var model = new Model();
            var principalEntityType = model.AddEntityType(typeof(OneToManyPrincipal));
            var property = principalEntityType.AddProperty(NavigationBase.IdProperty);
            var pk = principalEntityType.GetOrSetPrimaryKey(property);

            var dependentEntityType = model.AddEntityType(typeof(OneToManyDependent));
            var fkProp = dependentEntityType.AddProperty(NavigationBase.IdProperty);
            var fk = dependentEntityType.AddForeignKey(new[] { fkProp }, pk, principalEntityType);

            principalEntityType.AddNavigation("OneToManyDependents", fk, pointsToPrincipal: false);
            dependentEntityType.AddNavigation("OneToManyPrincipal", fk, pointsToPrincipal: true);
            return fk;
        }
        private static Model BuildModel()
        {
            var model = new Model();

            var entityType = model.AddEntityType(typeof(Banana));
            var property1 = entityType.AddProperty("P1", typeof(int));
            property1.IsShadowProperty = false;
            var property2 = entityType.AddProperty("P2", typeof(int?));
            property2.IsShadowProperty = false;

            entityType.GetOrSetPrimaryKey(property1);
            entityType.GetOrAddForeignKey(property2, entityType.FindPrimaryKey(), entityType);

            return model;
        }
        public void Can_ignore_existing_entity_type_using_entity_type_name()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(Customer).FullName);
            var modelBuilder = CreateModelBuilder(model);

            Assert.Same(entityType, modelBuilder.Entity(typeof(Customer).FullName, ConfigurationSource.Convention).Metadata);
            Assert.False(modelBuilder.Ignore(typeof(Customer).FullName, ConfigurationSource.DataAnnotation));
            Assert.NotNull(model.FindEntityType(typeof(Customer).FullName));

            Assert.NotNull(modelBuilder.Ignore(typeof(Customer).FullName, ConfigurationSource.Explicit));

            Assert.Null(model.FindEntityType(typeof(Customer).FullName));
        }
Example #17
0
        private static Model BuildModel()
        {
            var model = new Model();

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

            var idProperty = entityType.AddProperty("Id", typeof(int));
            idProperty.IsShadowProperty = false;
            idProperty.IsConcurrencyToken = true;
            idProperty.RequiresValueGenerator = true;
            var key = entityType.GetOrSetPrimaryKey(idProperty);

            entityType.AddProperty("Name", typeof(string)).IsShadowProperty = false;
            var property2 = entityType.AddProperty("State", typeof(string));
            property2.IsShadowProperty = false;
            property2.IsConcurrencyToken = true;

            var fkProperty = entityType.AddProperty("Fk", typeof(int?));
            fkProperty.IsShadowProperty = false;
            fkProperty.IsConcurrencyToken = true;
            entityType.GetOrAddForeignKey(fkProperty, key, entityType);

            var entityType2 = model.AddEntityType(typeof(SomeDependentEntity));
            var key2A = entityType2.AddProperty("Id1", typeof(int));
            key2A.IsShadowProperty = false;
            var key2B = entityType2.AddProperty("Id2", typeof(string));
            key2B.IsShadowProperty = false;
            entityType2.GetOrSetPrimaryKey(new[] { key2A, key2B });
            var fk = entityType2.AddProperty("SomeEntityId", typeof(int));
            fk.IsShadowProperty = false;
            entityType2.GetOrAddForeignKey(new[] { fk }, key, entityType);
            var justAProperty = entityType2.AddProperty("JustAProperty", typeof(int));
            justAProperty.IsShadowProperty = false;
            justAProperty.RequiresValueGenerator = true;

            var entityType5 = model.AddEntityType(typeof(SomeMoreDependentEntity));
            var key5 = entityType5.AddProperty("Id", typeof(int));
            key5.IsShadowProperty = false;
            entityType5.GetOrSetPrimaryKey(key5);
            var fk5A = entityType5.AddProperty("Fk1", typeof(int));
            fk5A.IsShadowProperty = false;
            var fk5B = entityType5.AddProperty("Fk2", typeof(string));
            fk5B.IsShadowProperty = false;
            entityType5.GetOrAddForeignKey(new[] { fk5A, fk5B }, entityType2.FindPrimaryKey(), entityType2);

            return model;
        }
        public virtual void Passes_on_foreign_key_cycle_with_one_GenerateOnAdd()
        {
            var model = new Model();
            var entityA = model.AddEntityType(typeof(A));
            SetPrimaryKey(entityA);
            var keyA = CreateKey(entityA);
            var entityB = model.AddEntityType(typeof(B));
            SetPrimaryKey(entityB);
            var keyB = CreateKey(entityB);

            CreateForeignKey(keyA, keyB);
            CreateForeignKey(keyB, keyA);

            keyA.Properties[0].RequiresValueGenerator = true;

            Validate(model);
        }
        private static Model BuildModel()
        {
            var model = new Model();

            var entityType = model.AddEntityType(typeof(Banana));
            var pkProperty = entityType.AddProperty("Id", typeof(int));
            pkProperty.IsShadowProperty = false;
            var fkProperty = entityType.AddProperty("Fk", typeof(int));
            fkProperty.IsShadowProperty = false;

            entityType.GetOrSetPrimaryKey(pkProperty);
            var fk = entityType.GetOrAddForeignKey(fkProperty, entityType.FindPrimaryKey(), entityType);

            entityType.AddProperty("Name", typeof(string)).IsShadowProperty = false;

            fk.HasPrincipalToDependent("LesserBananas");
            fk.HasDependentToPrincipal("TopBanana");

            return model;
        }
        private ForeignKey CreateOneToManySameBaseFK()
        {
            var model = new Model();

            var baseEntityType = model.AddEntityType(typeof(NavigationBase));
            var pk = baseEntityType.GetOrSetPrimaryKey(baseEntityType.GetOrAddProperty("Id", typeof(int)));

            var principalEntityType = model.AddEntityType(typeof(OneToManyPrincipal));
            principalEntityType.BaseType = baseEntityType;

            var dependentEntityType = model.AddEntityType(typeof(OneToManyDependent));
            dependentEntityType.BaseType = baseEntityType;
            var fkProp = dependentEntityType.GetOrAddProperty("Fk", typeof(int), shadowProperty: true);
            var fk = dependentEntityType.AddForeignKey(new[] { fkProp }, pk, principalEntityType);

            principalEntityType.AddNavigation("OneToManyDependents", fk, pointsToPrincipal: false);
            dependentEntityType.AddNavigation("OneToManyPrincipal", fk, pointsToPrincipal: true);
            return fk;
        }
        public virtual void Pases_on_diamond_path_to_root_principal_property()
        {
            var model = new Model();
            var entityA = model.AddEntityType(typeof(A));
            SetPrimaryKey(entityA);
            var keyA1 = CreateKey(entityA);
            var keyA2 = CreateKey(entityA, startingPropertyIndex: 0, propertyCount: 2);
            var keyA3 = CreateKey(entityA);
            var keyA4 = CreateKey(entityA, startingPropertyIndex: 2, propertyCount: 2);
            var entityB = model.AddEntityType(typeof(B));
            SetPrimaryKey(entityB);
            var keyB1 = CreateKey(entityB);
            var keyB2 = CreateKey(entityB, startingPropertyIndex: 1, propertyCount: 2);

            CreateForeignKey(keyA1, keyB1);
            CreateForeignKey(keyA2, keyB2);

            CreateForeignKey(keyB1, keyA3);
            CreateForeignKey(keyB2, keyA4);

            Validate(model);
        }
        private static Model BuildModel()
        {
            var model = new Model();

            var entityType = model.AddEntityType(typeof(Banana));
            var property1 = entityType.AddProperty("P1", typeof(int));
            property1.IsShadowProperty = false;
            var property2 = entityType.AddProperty("P2", typeof(string));
            property2.IsShadowProperty = false;
            var property3 = entityType.AddProperty("P3", typeof(Random));
            property3.IsShadowProperty = false;
            var property4 = entityType.AddProperty("P4", typeof(int));
            property4.IsShadowProperty = false;
            var property5 = entityType.AddProperty("P5", typeof(string));
            property5.IsShadowProperty = false;
            var property6 = entityType.AddProperty("P6", typeof(Random));
            property6.IsShadowProperty = false;

            entityType.GetOrSetPrimaryKey(new[] { property1, property2, property3 });
            entityType.GetOrAddForeignKey(new[] { property4, property5, property6 }, entityType.FindPrimaryKey(), entityType);

            return model;
        }