public void Apply_should_not_match_key_that_is_also_an_fk()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));

            entityType.AddKeyMember(property);

            var associationType
                = model.AddAssociationType(
                    "A", new EntityType("E", "N", DataSpace.CSpace), RelationshipMultiplicity.ZeroOrOne,
                    entityType, RelationshipMultiplicity.Many);

            associationType.Constraint
                = new ReferentialConstraint(
                    associationType.SourceEnd,
                    associationType.TargetEnd,
                    new[] { property },
                    new[] { property });

            (new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new DbModel(model, null));

            Assert.Null(entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void Can_get_list_of_key_properties()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            Assert.Empty(entityType.KeyProperties);

            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddKeyMember(property);

            Assert.Equal(1, entityType.KeyProperties.Count);
            Assert.Equal(1, entityType.KeyProperties.Where(key => entityType.DeclaredMembers.Contains(key)).Count());

            entityType.RemoveMember(property);

            var baseType = new EntityType("E", "N", DataSpace.CSpace);

            baseType.AddKeyMember(property);

            entityType.BaseType = baseType;

            Assert.Equal(1, entityType.KeyProperties.Count);
            Assert.Empty(entityType.KeyProperties.Where(key => entityType.DeclaredMembers.Contains(key)));
            Assert.Equal(1, entityType.KeyMembers.Count);
        }
        public void Apply_should_set_correct_defaults_for_string_keys()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            entityType.AddMember(property);
            entityType.AddKeyMember(property);

            ((IEdmConvention<EntityType>)new PropertyMaxLengthConvention())
                .Apply(entityType, new EdmModel(DataSpace.CSpace));

            Assert.Equal(128, property.MaxLength);
        }
示例#4
0
 public static System.Data.Entity.Core.Metadata.Edm.EntityType AddTable(
     this EdmModel database,
     string name,
     System.Data.Entity.Core.Metadata.Edm.EntityType pkSource)
 {
     System.Data.Entity.Core.Metadata.Edm.EntityType entityType = database.AddTable(name);
     foreach (EdmProperty keyProperty in pkSource.KeyProperties)
     {
         entityType.AddKeyMember((EdmMember)keyProperty.Clone());
     }
     return(entityType);
 }
        public void Apply_should_set_correct_defaults_for_string_keys()
        {
            var entityType = new EntityType();
            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            entityType.AddMember(property);
            entityType.AddKeyMember(property);

            ((IEdmConvention<EntityType>)new SqlCePropertyMaxLengthConvention())
                .Apply(entityType, CreateEdmModel());

            Assert.Equal(4000, property.MaxLength);
        }
        public void Apply_should_match_simple_short_key()
        {
            var entityType = new EntityType();
            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
            entityType.AddKeyMember(property);

            ((IEdmConvention<EntityType>)new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new EdmModel().Initialize());

            Assert.Equal(
                StoreGeneratedPattern.Identity,
                entityType.DeclaredKeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void Apply_should_match_simple_short_key()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
            entityType.AddKeyMember(property);

            (new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null));

            Assert.Equal(
                StoreGeneratedPattern.Identity,
                entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void FromProperties_lazy_loaded_when_none_present()
        {
            var principalEntity = new EntityType("E", "N", DataSpace.CSpace);
            principalEntity.AddKeyMember(new EdmProperty("K"));

            var referentialConstraint
                = new ReferentialConstraint(
                    new AssociationEndMember("P", principalEntity),
                    new AssociationEndMember("D", new EntityType("E", "N", DataSpace.CSpace)),
                    Enumerable.Empty<EdmProperty>(),
                    Enumerable.Empty<EdmProperty>());

            Assert.NotEmpty(referentialConstraint.FromProperties);
        }
示例#9
0
        public void FromProperties_lazy_loaded_when_none_present()
        {
            var principalEntity = new EntityType("E", "N", DataSpace.CSpace);

            principalEntity.AddKeyMember(new EdmProperty("K"));

            var referentialConstraint
                = new ReferentialConstraint(
                      new AssociationEndMember("P", principalEntity),
                      new AssociationEndMember("D", new EntityType("E", "N", DataSpace.CSpace)),
                      Enumerable.Empty <EdmProperty>(),
                      Enumerable.Empty <EdmProperty>());

            Assert.NotEmpty(referentialConstraint.FromProperties);
        }
        public void Apply_should_move_declared_keys_head_of_declared_properties_list()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var type = typeof(SimpleEntity);

            entityType.GetMetadataProperties().SetClrType(type);

            var property1 = EdmProperty.CreatePrimitive("PrivateProperty", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);

            var property2 = EdmProperty.CreatePrimitive("InheritedPropertyB", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property2);

            var property3 = EdmProperty.CreatePrimitive("InheritedPropertyA", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property3);

            var property4 = EdmProperty.CreatePrimitive("PropertyB", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property4);

            var property5 = EdmProperty.CreatePrimitive("PropertyA", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property5);

            var property6 = EdmProperty.CreatePrimitive("Key", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property6);

            entityType.AddKeyMember(property6);

            new DeclaredPropertyOrderingConvention().Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null));

            Assert.True(
                entityType.DeclaredProperties.Select(e => e.Name)
                    .SequenceEqual(
                        new[]
                            {
                                "Key",
                                "PrivateProperty",
                                "PropertyA",
                                "PropertyB",
                                "InheritedPropertyA",
                                "InheritedPropertyB"
                            }));
        }
        public void Can_remove_member()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            Assert.Empty(entityType.Members);

            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddKeyMember(property);

            Assert.Equal(1, entityType.KeyMembers.Count);
            Assert.Equal(1, entityType.Members.Count);

            entityType.RemoveMember(property);

            Assert.Empty(entityType.KeyMembers);
            Assert.Empty(entityType.Members);
        }
示例#12
0
        public void IsKeyMember_should_return_true_when_part_of_key()
        {
            var primitiveType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32);

            var property = EdmProperty.Primitive("P", primitiveType);

            Assert.False(property.IsKeyMember);

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            entityType.AddMember(property);

            Assert.False(property.IsKeyMember);

            entityType.AddKeyMember(property);

            Assert.True(property.IsKeyMember);
        }
        public void Can_remove_member()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            Assert.Empty(entityType.Members);

            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddKeyMember(property);

            Assert.Equal(1, entityType.KeyMembers.Count);
            Assert.Equal(1, entityType.Members.Count);

            entityType.RemoveMember(property);

            Assert.Empty(entityType.KeyMembers);
            Assert.Empty(entityType.Members);
        }
        public void Apply_should_introduce_constraint_when_one_to_one()
        {
            var entityType1 = new EntityType("E", "N", DataSpace.CSpace);
            entityType1.AddKeyMember(EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)));

            var entityType2 = new EntityType("E", "N", DataSpace.CSpace);
            entityType2.AddKeyMember(EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)));

            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);
            associationType.SourceEnd = new AssociationEndMember("S", entityType2);
            associationType.TargetEnd = new AssociationEndMember("T", entityType1);

            associationType.MarkPrincipalConfigured();

            ((IEdmConvention<AssociationType>)new OneToOneConstraintIntroductionConvention())
                .Apply(associationType, new EdmModel(DataSpace.CSpace));

            Assert.NotNull(associationType.Constraint);
            Assert.Equal(1, associationType.Constraint.ToProperties.Count);
        }
        public void KeyProperties_is_thread_safe()
        {
            var baseType   = new EntityType("E", "N", DataSpace.CSpace);
            var entityType = new EntityType("F", "N", DataSpace.CSpace);
            var property   = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            baseType.AddKeyMember(property);
            entityType.BaseType = baseType;
            const int cycles      = 100;
            const int threadCount = 30;

            Action readKeyProperty = () =>
            {
                for (var i = 0; i < cycles; ++i)
                {
                    var keys = entityType.KeyProperties;

                    //touching KeyMembers.Source triggers a reset to KeyProperties
                    var sourceCount = entityType.KeyMembers.Source.Count;
                    Assert.True(sourceCount == 1);

                    var keysAfterReset = entityType.KeyProperties;

                    Assert.True(keys != null, "First reference to key properties should not be null");
                    Assert.True(keysAfterReset != null, "Second reference to key properties should not be null");
                    Assert.False(ReferenceEquals(keys, keysAfterReset), "The key properties instances should be different");
                }
            };

            var tasks = new List <Thread>();

            for (var i = 0; i < threadCount; ++i)
            {
                var thread = new Thread(new ThreadStart(readKeyProperty));
                tasks.Add(thread);
            }

            tasks.ForEach(t => t.Start());
            tasks.ForEach(t => t.Join());
        }
        public void KeyProperties_is_thread_safe()
        {
            var baseType = new EntityType("E", "N", DataSpace.CSpace);
            var entityType = new EntityType("F", "N", DataSpace.CSpace);
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            baseType.AddKeyMember(property);
            entityType.BaseType = baseType;
            const int cycles = 100;
            const int threadCount = 30;

            Action readKeyProperty = () =>
            {
                for (var i = 0; i < cycles; ++i)
                {
                    var keys = entityType.KeyProperties;

                    //touching KeyMembers.Source triggers a reset to KeyProperties
                    var sourceCount = entityType.KeyMembers.Source.Count;
                    Assert.True(sourceCount == 1);

                    var keysAfterReset = entityType.KeyProperties;

                    Assert.True(keys != null, "First reference to key properties should not be null");
                    Assert.True(keysAfterReset != null, "Second reference to key properties should not be null");
                    Assert.False(ReferenceEquals(keys, keysAfterReset), "The key properties instances should be different");
                }
            };

            var tasks = new List<Thread>();
            for (var i = 0; i < threadCount; ++i)
            {
                var thread = new Thread(new ThreadStart(readKeyProperty));
                tasks.Add(thread);
            }

            tasks.ForEach(t => t.Start());
            tasks.ForEach(t => t.Join());
        }
示例#17
0
        public void Can_get_list_of_declared_key_properties()
        {
            var entityType = new EntityType();

            Assert.Empty(entityType.DeclaredKeyProperties);

            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddKeyMember(property);

            Assert.Equal(1, entityType.DeclaredKeyProperties.Count);

            entityType.RemoveMember(property);

            var baseType = new EntityType();

            baseType.AddKeyMember(property);

            entityType.BaseType = baseType;

            Assert.Empty(entityType.DeclaredKeyProperties);
            Assert.Equal(1, entityType.KeyMembers.Count);
        }
        public void Can_get_list_of_key_properties()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            Assert.Empty(entityType.KeyProperties);

            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddKeyMember(property);

            Assert.Equal(1, entityType.KeyProperties.Count);
            Assert.Equal(1, entityType.KeyProperties.Where(key => entityType.DeclaredMembers.Contains(key)).Count());

            entityType.RemoveMember(property);

            var baseType = new EntityType("E", "N", DataSpace.CSpace);
            baseType.AddKeyMember(property);

            entityType.BaseType = baseType;

            Assert.Equal(1, entityType.KeyProperties.Count);
            Assert.Empty(entityType.KeyProperties.Where(key => entityType.DeclaredMembers.Contains(key)));
            Assert.Equal(1, entityType.KeyMembers.Count);
        }
        public void Can_generate_function_mappings_for_entity_type()
        {
            var functionMappingGenerator
                = new ModificationFunctionMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest);

            var databaseMapping
                = new DbDatabaseMapping()
                    .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            var intProperty = EdmProperty.CreatePrimitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
            entityType.AddKeyMember(intProperty);

            var stringProperty = EdmProperty.CreatePrimitive("Name", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            entityType.AddMember(stringProperty);

            var entitySetMapping
                = databaseMapping.AddEntitySetMapping(
                    databaseMapping.Model.AddEntitySet("ES", entityType));

            var storageEntityTypeMapping 
                = new EntityTypeMapping(
                    new EntitySetMapping(new EntitySet(), databaseMapping.EntityContainerMappings.Single()));
           
            storageEntityTypeMapping.AddType(entityType);
            
            var storageMappingFragment = new MappingFragment(new EntitySet(), storageEntityTypeMapping, false);
            
            storageMappingFragment.AddColumnMapping(
                new ColumnMappingBuilder(new EdmProperty("C0", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { intProperty }));

            storageMappingFragment.AddColumnMapping(
                new ColumnMappingBuilder(new EdmProperty("C1", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { stringProperty }));

            storageEntityTypeMapping.AddFragment(storageMappingFragment);

            entitySetMapping.AddTypeMapping(storageEntityTypeMapping);

            functionMappingGenerator.Generate(entityType, databaseMapping);

            var modificationFunctionMapping
                = entitySetMapping.ModificationFunctionMappings.Single();

            Assert.NotNull(modificationFunctionMapping);

            var functionMapping = modificationFunctionMapping.InsertFunctionMapping;

            Assert.NotNull(functionMapping);
            Assert.Equal(2, functionMapping.ParameterBindings.Count);
            Assert.Null(functionMapping.ResultBindings);

            var function = functionMapping.Function;

            Assert.NotNull(function);
            Assert.Equal("E_Insert", function.Name);
            Assert.Equal(2, function.Parameters.Count);

            functionMapping = modificationFunctionMapping.UpdateFunctionMapping;

            Assert.NotNull(functionMapping);
            Assert.Equal(2, functionMapping.ParameterBindings.Count);
            Assert.Null(functionMapping.ResultBindings);

            function = functionMapping.Function;

            Assert.NotNull(function);
            Assert.Equal("E_Update", function.Name);
            Assert.Equal(2, function.Parameters.Count);

            functionMapping = modificationFunctionMapping.DeleteFunctionMapping;

            Assert.NotNull(functionMapping);
            Assert.Equal(1, functionMapping.ParameterBindings.Count);
            Assert.Null(functionMapping.ResultBindings);

            function = modificationFunctionMapping.DeleteFunctionMapping.Function;

            Assert.NotNull(function);
            Assert.Equal("E_Delete", function.Name);
            Assert.Equal(1, function.Parameters.Count);
        }
        public void Apply_should_set_given_value_for_binary_key()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            entityType.AddMember(property);
            entityType.AddKeyMember(property);

            (new SqlCePropertyMaxLengthConvention(2000)).Apply(entityType, CreateDbModel());

            Assert.Null(property.IsUnicode);
            Assert.Equal(2000, property.MaxLength);
        }
示例#21
0
        public void Apply_should_set_correct_defaults_for_binary_key()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Binary));
            entityType.AddMember(property);
            entityType.AddKeyMember(property);

            (new PropertyMaxLengthConvention())
                .Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null));

            Assert.Null(property.IsUnicode);
            Assert.Equal(128, property.MaxLength);
        }
        public void IsKeyMember_should_return_true_when_part_of_key()
        {
            var primitiveType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32);

            var property = EdmProperty.CreatePrimitive("P", primitiveType);

            Assert.False(property.IsKeyMember);

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            entityType.AddMember(property);

            Assert.False(property.IsKeyMember);

            entityType.AddKeyMember(property);

            Assert.True(property.IsKeyMember);
        }
示例#23
0
        public void Apply_should_ignore_when_key_already_specified()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property1 = EdmProperty.Primitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);
            var property = property1;
            entityType.AddKeyMember(EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)));

            (new IdKeyDiscoveryConvention()).Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null));

            Assert.False(entityType.KeyProperties.Contains(property));
        }
        public void Apply_should_update_foreign_keys()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var principalProperty = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            principalProperty.MaxLength = 23;
            entityType.AddMember(principalProperty);
            entityType.AddKeyMember(principalProperty);

            var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);
            associationType.SourceEnd = new AssociationEndMember("S", entityType);
            associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace));

            var dependentProperty = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            associationType.Constraint
                = new ReferentialConstraint(
                    associationType.SourceEnd,
                    associationType.TargetEnd,
                    new[] { principalProperty },
                    new[] { dependentProperty });

            ((IEdmConvention<AssociationType>)new PropertyMaxLengthConvention())
                .Apply(associationType, new EdmModel(DataSpace.CSpace));

            Assert.Equal(23, dependentProperty.MaxLength);
        }
        public void Apply_should_not_match_simple_key_of_wrong_type()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            entityType.AddKeyMember(property);

            ((IEdmConvention<EntityType>)new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new EdmModel(DataSpace.CSpace));

            Assert.Null(entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
        private void ConfigureKey(EntityType entityType)
        {
            DebugCheck.NotNull(entityType);

            if (!_keyProperties.Any())
            {
                return;
            }

            if (entityType.BaseType != null)
            {
                throw Error.KeyRegisteredOnDerivedType(ClrType, entityType.GetRootType().GetClrType());
            }

            var keyProperties = _keyProperties.AsEnumerable();

            if (!_isKeyConfigured)
            {
                var primaryKeys
                    = from p in _keyProperties
                      select new
                          {
                              PropertyInfo = p,
                              Property(new PropertyPath(p)).ColumnOrder
                          };

                if ((_keyProperties.Count > 1)
                    && primaryKeys.Any(p => !p.ColumnOrder.HasValue))
                {
                    throw Error.ModelGeneration_UnableToDetermineKeyOrder(ClrType);
                }

                keyProperties = primaryKeys.OrderBy(p => p.ColumnOrder).Select(p => p.PropertyInfo);
            }

            foreach (var keyProperty in keyProperties)
            {
                var property = entityType.GetDeclaredPrimitiveProperty(keyProperty);

                if (property == null)
                {
                    throw Error.KeyPropertyNotFound(keyProperty.Name, entityType.Name);
                }

                property.Nullable = false;
                entityType.AddKeyMember(property);
            }
        }
        public void Apply_should_match_optional_self_ref()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));

            entityType.AddKeyMember(property);

            model.AddAssociationType(
                    "A",
                    entityType, RelationshipMultiplicity.ZeroOrOne,
                    entityType, RelationshipMultiplicity.Many);

            (new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new DbModel(model, null));

            Assert.NotNull(entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void Apply_should_not_match_composite_key()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddKeyMember(property);
            entityType.AddKeyMember(EdmProperty.CreatePrimitive("K", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)));

            (new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null));

            Assert.Equal(
                0,
                entityType.KeyProperties
                    .Count(p => p.GetStoreGeneratedPattern() == StoreGeneratedPattern.Identity));
        }
        public void Generate_should_exclude_sgp_properties_from_corresponding_function_mappings()
        {
            var functionMappingGenerator
                = new ModificationFunctionMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest);

            var databaseMapping
                = new DbDatabaseMapping()
                    .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            var intProperty = EdmProperty.Primitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
            intProperty.SetStoreGeneratedPattern(StoreGeneratedPattern.Identity);
            entityType.AddKeyMember(intProperty);

            var stringProperty = EdmProperty.Primitive("Name", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            stringProperty.SetStoreGeneratedPattern(StoreGeneratedPattern.Computed);
            entityType.AddMember(stringProperty);

            var entitySetMapping
                = databaseMapping.AddEntitySetMapping(
                    databaseMapping.Model.AddEntitySet("ES", entityType));

            var storageEntityTypeMapping
                = new StorageEntityTypeMapping(
                    new StorageEntitySetMapping(new EntitySet(), databaseMapping.EntityContainerMappings.Single()));

            storageEntityTypeMapping.AddType(entityType);

            var storageMappingFragment = new StorageMappingFragment(new EntitySet(), storageEntityTypeMapping, false);

            storageMappingFragment.AddColumnMapping(
                new ColumnMappingBuilder(new EdmProperty("C0"), new[] { intProperty }));

            storageMappingFragment.AddColumnMapping(
                new ColumnMappingBuilder(new EdmProperty("C1"), new[] { stringProperty }));

            storageEntityTypeMapping.AddFragment(storageMappingFragment);

            entitySetMapping.AddTypeMapping(storageEntityTypeMapping);

            functionMappingGenerator.Generate(entityType, databaseMapping);

            var modificationFunctionMapping
                = entitySetMapping.ModificationFunctionMappings.Single();

            Assert.NotNull(modificationFunctionMapping);

            var functionMapping = modificationFunctionMapping.InsertFunctionMapping;

            Assert.NotNull(functionMapping);
            Assert.Equal(0, functionMapping.ParameterBindings.Count);
            Assert.Equal(2, functionMapping.ResultBindings.Count);

            var function = functionMapping.Function;

            Assert.NotNull(function);
            Assert.Equal("E_Insert", function.Name);
            Assert.Equal(0, function.Parameters.Count);

            functionMapping = modificationFunctionMapping.UpdateFunctionMapping;

            Assert.NotNull(functionMapping);
            Assert.Equal(1, functionMapping.ParameterBindings.Count);
            Assert.Equal(1, functionMapping.ResultBindings.Count);

            function = functionMapping.Function;

            Assert.NotNull(function);
            Assert.Equal("E_Update", function.Name);
            Assert.Equal(1, function.Parameters.Count);

            functionMapping = modificationFunctionMapping.DeleteFunctionMapping;

            Assert.NotNull(functionMapping);
            Assert.Equal(1, functionMapping.ParameterBindings.Count);
            Assert.Null(functionMapping.ResultBindings);

            function = modificationFunctionMapping.DeleteFunctionMapping.Function;

            Assert.NotNull(function);
            Assert.Equal("E_Delete", function.Name);
            Assert.Equal(1, function.Parameters.Count);
        }
        private IEnumerable<EdmProperty> GenerateIndependentForeignKeyColumns(
            EntityType principalEntityType,
            EntityType dependentEntityType,
            AssociationSetMapping associationSetMapping,
            EndPropertyMapping associationEndMapping,
            EntityType dependentTable,
            bool isPrimaryKeyColumn,
            NavigationProperty principalNavigationProperty)
        {
            DebugCheck.NotNull(principalEntityType);
            DebugCheck.NotNull(associationEndMapping);
            DebugCheck.NotNull(dependentTable);

            foreach (var property in principalEntityType.KeyProperties())
            {
                var columnName
                    = ((principalNavigationProperty != null)
                           ? principalNavigationProperty.Name
                           : principalEntityType.Name) + "_" + property.Name;

                var foreignKeyColumn
                    = MapTableColumn(property, columnName, false);

                dependentTable.AddColumn(foreignKeyColumn);

                if (isPrimaryKeyColumn)
                {
                    dependentTable.AddKeyMember(foreignKeyColumn);
                }

                foreignKeyColumn.Nullable
                    = associationEndMapping.AssociationEnd.IsOptional()
                      || (associationEndMapping.AssociationEnd.IsRequired()
                          && dependentEntityType.BaseType != null);

                foreignKeyColumn.StoreGeneratedPattern = StoreGeneratedPattern.None;

                yield return foreignKeyColumn;

                associationEndMapping.AddPropertyMapping(new ScalarPropertyMapping(property, foreignKeyColumn));

                if (foreignKeyColumn.Nullable)
                {
                    associationSetMapping
                        .AddCondition(new IsNullConditionMapping(foreignKeyColumn, false));
                }
            }
        }
        public void Apply_should_match_key_that_is_an_fk_used_in_table_splitting()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));

            entityType.AddKeyMember(property);

            var targetConfig = new EntityTypeConfiguration(typeof(object));
            targetConfig.ToTable("SharedTable");
            entityType.GetMetadataProperties().SetConfiguration(targetConfig);

            var sourceEntityType = new EntityType("E", "N", DataSpace.CSpace);
            var sourceConfig = new EntityTypeConfiguration(typeof(object));
            sourceConfig.ToTable("SharedTable");
            sourceEntityType.GetMetadataProperties().SetConfiguration(sourceConfig);

            var associationType
                = model.AddAssociationType(
                    "A", sourceEntityType, RelationshipMultiplicity.One,
                    entityType, RelationshipMultiplicity.One);

            associationType.Constraint
                = new ReferentialConstraint(
                    associationType.SourceEnd,
                    associationType.TargetEnd,
                    new[] { property },
                    new[] { property });

            (new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new DbModel(model, null));

            Assert.Equal(
                StoreGeneratedPattern.Identity,
                entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void Apply_should_not_match_composite_key()
        {
            var entityType = new EntityType();
            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddKeyMember(property);
            entityType.AddKeyMember(EdmProperty.Primitive("K", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)));

            ((IEdmConvention<EntityType>)new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new EdmModel().Initialize());

            Assert.Equal(
                0,
                entityType.DeclaredKeyProperties
                    .Count(p => p.GetStoreGeneratedPattern() == StoreGeneratedPattern.Identity));
        }