Exemplo n.º 1
0
        public EdmProperty BuildString(string name, bool isUnicode, bool isFixedLength, int maxLength)
        {
            var typeUsage = TypeUsage.CreateStringTypeUsage(
                PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String),
                isUnicode,
                isFixedLength,
                maxLength);

            var property = EdmProperty.Create(
                name,
                typeUsage);

            return(property);
        }
        public void Visit_DbIsNullExpression_creates_equivalent_legacy_DbIsNullExpression()
        {
            var isNullExpression =
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)).Null().IsNull();

            var legacyIsNullExpression =
                _legacyDbExpressionConverter.Visit(isNullExpression) as LegacyCommandTrees.DbIsNullExpression;

            Assert.NotNull(legacyIsNullExpression);
            Assert.Equal(LegacyCommandTrees.DbExpressionKind.IsNull, legacyIsNullExpression.ExpressionKind);
            Assert.Equal(LegacyCommandTrees.DbExpressionKind.Null, legacyIsNullExpression.Argument.ExpressionKind);
            TypeUsageVerificationHelper
            .VerifyTypeUsagesEquivalent(legacyIsNullExpression.ResultType, isNullExpression.ResultType);
        }
        public void GetComplexPropertyMappings_should_return_all_complex_property_mappings_for_type()
        {
            var databaseMapping = new DbDatabaseMapping()
                                  .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));
            var entitySet = new EntitySet
            {
                Name = "ES"
            };
            var entitySetMapping  = databaseMapping.AddEntitySetMapping(entitySet);
            var entityTypeMapping = new EntityTypeMapping(null);

            entitySetMapping.AddTypeMapping(entityTypeMapping);
            var entityTypeMappingFragment = new MappingFragment(entitySet, entityTypeMapping, false);

            entityTypeMapping.AddFragment(entityTypeMappingFragment);
            var complexType = new ComplexType("C");
            var propertyMapping1
                = new ColumnMappingBuilder(
                      new EdmProperty("C", TypeUsage.Create(new PrimitiveType()
            {
                DataSpace = DataSpace.SSpace
            })),
                      new[]
            {
                EdmProperty.CreateComplex("P1", complexType),
                EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))
            });
            var type = typeof(object);

            complexType.GetMetadataProperties().SetClrType(type);

            entityTypeMappingFragment.AddColumnMapping(propertyMapping1);

            var propertyMapping2
                = new ColumnMappingBuilder(
                      new EdmProperty("C", TypeUsage.Create(new PrimitiveType()
            {
                DataSpace = DataSpace.SSpace
            })),
                      new List <EdmProperty>
            {
                EdmProperty.CreateComplex("P3", complexType),
                EdmProperty.CreatePrimitive(
                    "P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
            });

            entityTypeMappingFragment.AddColumnMapping(propertyMapping2);

            Assert.Equal(2, databaseMapping.GetComplexPropertyMappings(typeof(object)).Count());
        }
Exemplo n.º 4
0
        public void Apply_should_set_correct_defaults_for_unconfigured_binary()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property   = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Binary));

            entityType.AddMember(property);

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

            Assert.Null(property.IsUnicode);
            Assert.Equal(false, property.IsFixedLength);
            Assert.Null(property.MaxLength);
        }
Exemplo n.º 5
0
        public void ComplexType_apply_should_set_correct_defaults_for_unconfigured_binary()
        {
            var entityType = new ComplexType("C");
            var property   = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Binary));

            entityType.AddMember(property);

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

            Assert.Null(property.IsUnicode);
            Assert.Equal(false, property.IsFixedLength);
            Assert.Null(property.MaxLength);
        }
Exemplo n.º 6
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);
        }
        /// <summary>
        ///     Creates a SQLiteParameter given a name, type, and direction
        /// </summary>
        internal static SQLiteParameter CreateSQLiteParameter(string name, TypeUsage type, ParameterMode mode,
                                                              object value)
        {
            int?size;

            if (type.GetPrimitiveTypeKind() == PrimitiveTypeKind.Guid)
            {
                type = TypeUsage.CreateStringTypeUsage(
                    PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String),
                    false, true);
            }

            var result = new SQLiteParameter(name, value);

            // .Direction
            var direction = MetadataHelpers.ParameterModeToParameterDirection(mode);

            if (result.Direction != direction)
            {
                result.Direction = direction;
            }

            // .Size and .DbType
            // output parameters are handled differently (we need to ensure there is space for return
            // values where the user has not given a specific Size/MaxLength)
            var isOutParam = mode != ParameterMode.In;
            var sqlDbType  = GetSQLiteDbType(type, isOutParam, out size);

            if (result.DbType != sqlDbType)
            {
                result.DbType = sqlDbType;
            }

            // Note that we overwrite 'facet' parameters where either the value is different or
            // there is an output parameter.
            if (size.HasValue && (isOutParam || result.Size != size.Value))
            {
                result.Size = size.Value;
            }

            // .IsNullable
            var isNullable = type.GetIsNullable();

            if (isOutParam || isNullable != result.IsNullable)
            {
                result.IsNullable = isNullable;
            }

            return(result);
        }
Exemplo n.º 8
0
        public void Apply_should_ignore_when_type_is_derived()
        {
            var entityType = new EntityType();
            var property1  = EdmProperty.Primitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);
            var property = property1;

            entityType.BaseType = new EntityType();

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

            Assert.False(entityType.DeclaredKeyProperties.Contains(property));
        }
        public void Apply_should_match_simple_short_key()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property   = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));

            entityType.AddKeyMember(property);

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

            Assert.Equal(
                StoreGeneratedPattern.Identity,
                entityType.KeyProperties.Single().GetStoreGeneratedPattern());
        }
Exemplo n.º 10
0
        public void Generate_can_map_independent_association_type()
        {
            var model = new EdmModel(DataSpace.CSpace);
            var principalEntityType = model.AddEntityType("P");
            var type = typeof(object);

            principalEntityType.GetMetadataProperties().SetClrType(type);
            var property = EdmProperty.CreatePrimitive("Id1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            principalEntityType.AddMember(property);
            var idProperty1 = property;

            principalEntityType.AddKeyMember(idProperty1);
            var property1 = EdmProperty.CreatePrimitive("Id2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            principalEntityType.AddMember(property1);
            var idProperty2 = property1;

            principalEntityType.AddKeyMember(idProperty2);
            var dependentEntityType = model.AddEntityType("D");
            var type1 = typeof(string);

            dependentEntityType.GetMetadataProperties().SetClrType(type1);
            model.AddEntitySet("PSet", principalEntityType);
            model.AddEntitySet("DSet", dependentEntityType);
            var associationType
                = model.AddAssociationType(
                      "P_D",
                      principalEntityType, RelationshipMultiplicity.One,
                      dependentEntityType, RelationshipMultiplicity.Many);

            model.AddAssociationSet("P_DSet", associationType);
            associationType.SourceEnd.DeleteBehavior = OperationAction.Cascade;

            var databaseMapping = CreateDatabaseMappingGenerator().Generate(model);

            var foreignKeyConstraint
                =
                    databaseMapping.GetEntityTypeMapping(dependentEntityType).MappingFragments.Single().Table.ForeignKeyBuilders.Single();

            Assert.Equal(2, foreignKeyConstraint.DependentColumns.Count());
            Assert.Equal(associationType.Name, foreignKeyConstraint.Name);
            Assert.Equal(1, databaseMapping.EntityContainerMappings.Single().AssociationSetMappings.Count());
            Assert.Equal(OperationAction.Cascade, foreignKeyConstraint.DeleteAction);

            var foreignKeyColumn = foreignKeyConstraint.DependentColumns.First();

            Assert.False(foreignKeyColumn.Nullable);
            Assert.Equal("P_Id1", foreignKeyColumn.Name);
        }
Exemplo n.º 11
0
        public void HasStoreGeneratedPattern_should_return_true_when_not_null_or_none()
        {
            var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            Assert.False(property.HasStoreGeneratedPattern());

            property.SetStoreGeneratedPattern(StoreGeneratedPattern.None);

            Assert.False(property.HasStoreGeneratedPattern());

            property.SetStoreGeneratedPattern(StoreGeneratedPattern.Computed);

            Assert.True(property.HasStoreGeneratedPattern());
        }
        public void ComplexType_apply_should_set_correct_defaults_for_non_unicode_fixed_length_strings()
        {
            var entityType = new ComplexType("C");
            var property   = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            property.IsFixedLength = true;
            property.IsUnicode     = false;
            entityType.AddMember(property);

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

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

            property.IsFixedLength = true;
            entityType.AddMember(property);

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

            Assert.Null(property.IsUnicode);
            Assert.Equal(4000, property.MaxLength);
        }
Exemplo n.º 14
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)));

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

            Assert.False(entityType.KeyProperties.Contains(property));
        }
 private static EnumType CreateEnumType()
 {
     var enumTypeCtor =
         typeof(EnumType).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)
             .Single(c => c.GetParameters().Count() == 5);
     var enumType = (EnumType)enumTypeCtor.Invoke(BindingFlags.NonPublic | BindingFlags.Instance, null,
         new object[]
         {
             "TestEnumType", "Model", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32), false,
             DataSpace.CSpace
         },
         CultureInfo.InvariantCulture);
     return enumType;
 }
Exemplo n.º 16
0
        public void Apply_should_ignore_when_type_is_derived()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property1  = EdmProperty.Primitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);
            var property = property1;

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

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

            Assert.False(entityType.KeyProperties.Contains(property));
        }
Exemplo n.º 17
0
        public void Apply_should_match_simple_long_key()
        {
            var entityType = new EntityType();
            var property   = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));

            entityType.AddKeyMember(property);

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

            Assert.Equal(
                StoreGeneratedPattern.Identity,
                entityType.DeclaredKeyProperties.Single().GetStoreGeneratedPattern());
        }
        private EntityType CreateEntityTypeWithProperties(params PropertyInfo[] props)
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            foreach (var prop in props)
            {
                var property = EdmProperty.CreatePrimitive(prop.Name, PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

                entityType.AddMember(property);
                entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == prop.Name).SetClrPropertyInfo(prop);
            }

            return(entityType);
        }
Exemplo n.º 19
0
        public void ComplexType_apply_should_set_correct_defaults_for_fixed_length_binary()
        {
            var entityType = new ComplexType("C");
            var property   = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Binary));

            property.IsFixedLength = true;
            entityType.AddMember(property);

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

            Assert.Null(property.IsUnicode);
            Assert.Equal(128, property.MaxLength);
            Assert.False(property.IsMaxLength);
        }
Exemplo n.º 20
0
        public void ComplexType_apply_should_set_correct_defaults_for_unconfigured_strings()
        {
            var entityType = new ComplexType("C");
            var property   = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property);

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

            Assert.Equal(true, property.IsUnicode);
            Assert.Equal(false, property.IsFixedLength);
            Assert.Null(property.MaxLength);
            Assert.Equal(true, property.IsMaxLength);
        }
        public void Map_should_set_correct_name_and_type()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var mappingContext
                = new MappingContext(new ModelConfiguration(), new ConventionsConfiguration(), new EdmModel(DataSpace.CSpace));

            new PropertyMapper(new TypeMapper(mappingContext))
            .Map(new MockPropertyInfo(typeof(int), "Foo"), entityType, () => new EntityTypeConfiguration(typeof(object)));

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

            var property = entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == "Foo");

            Assert.Equal(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32), property.PrimitiveType);
        }
        public void Map_should_map_complex_type_properties()
        {
            var complexType = new ComplexType("C");
            var mappingContext
                = new MappingContext(new ModelConfiguration(), new ConventionsConfiguration(), new EdmModel(DataSpace.CSpace));

            new PropertyMapper(new TypeMapper(mappingContext))
            .Map(new MockPropertyInfo(typeof(int), "Foo"), complexType, () => new ComplexTypeConfiguration(typeof(object)));

            Assert.Equal(1, complexType.Properties.Count);

            var property = complexType.Properties.SingleOrDefault(p => p.Name == "Foo");

            Assert.Equal(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32), property.PrimitiveType);
        }
Exemplo n.º 23
0
        public void MetadataWorkspace_initialized_from_DbQueryCommandTree()
        {
            var workspace = new MetadataWorkspace();

            var commandTreeFacts =
                new CommandTreeFacts(
                    new DbQueryCommandTree(
                        workspace,
                        DataSpace.CSpace,
                        TypeUsage.CreateDefaultTypeUsage(
                            PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)).Null(),
                        validate: false));

            Assert.Same(workspace, commandTreeFacts.MetadataWorkspace);
        }
Exemplo n.º 24
0
        public void Apply_should_set_correct_defaults_for_non_unicode_fixed_length_strings()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var property   = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            property.IsFixedLength = true;
            property.IsUnicode     = false;
            entityType.AddMember(property);

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

            Assert.Equal(128, property.MaxLength);
            Assert.False(property.IsMaxLength);
        }
        public void Visit_DbParameterRefExpression_creates_equivalent_legacy_DbParameterRefExpression()
        {
            var paramRefExpression =
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))
                .Parameter("foo");

            var legacyParamRefExpression =
                _legacyDbExpressionConverter.Visit(paramRefExpression) as LegacyCommandTrees.DbParameterReferenceExpression;

            Assert.NotNull(legacyParamRefExpression);
            Assert.Equal(LegacyCommandTrees.DbExpressionKind.ParameterReference, legacyParamRefExpression.ExpressionKind);
            Assert.Equal("foo", legacyParamRefExpression.ParameterName);
            TypeUsageVerificationHelper
            .VerifyTypeUsagesEquivalent(legacyParamRefExpression.ResultType, paramRefExpression.ResultType);
        }
Exemplo n.º 26
0
 private static MetadataProperty CreateAnnotationMetadataProperty(string name, string value)
 {
     return
         (MetadataProperty.Create(
              string.Format(
                  CultureInfo.InvariantCulture,
                  "{0}:{1}",
                  SchemaManager.AnnotationNamespace,
                  name),
              TypeUsage.CreateStringTypeUsage(
                  PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String),
                  isUnicode: false,
                  isFixedLength: false),
              value));
 }
Exemplo n.º 27
0
        public void Apply_should_ignore_case()
        {
            var entityType = new EntityType
            {
                Name = "Foo"
            };
            var property1 = EdmProperty.Primitive("foOid", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            entityType.AddMember(property1);
            var property = property1;

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

            Assert.True(entityType.DeclaredKeyProperties.Contains(property));
        }
Exemplo n.º 28
0
        public void ComplexType_apply_should_set_correct_defaults_for_unicode_fixed_length_strings()
        {
            var entityType = new ComplexType("C");
            var property   = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            property.IsFixedLength = true;
            entityType.AddMember(property);

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

            Assert.Equal(true, property.IsUnicode);
            Assert.Equal(128, property.MaxLength);
            Assert.False(property.IsMaxLength);
        }
        public void Visit_DbCastExpression_creates_equivalent_legacy_DbCastExpression()
        {
            var castExpression =
                DbExpressionBuilder.Constant(42).CastTo(
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Decimal)));

            var legacyCastExpression =
                _legacyDbExpressionConverter.Visit(castExpression) as LegacyCommandTrees.DbCastExpression;

            Assert.NotNull(legacyCastExpression);
            Assert.Equal(LegacyCommandTrees.DbExpressionKind.Cast, legacyCastExpression.ExpressionKind);
            Assert.Equal(42, (int)((LegacyCommandTrees.DbConstantExpression)legacyCastExpression.Argument).Value);
            TypeUsageVerificationHelper
            .VerifyTypeUsagesEquivalent(legacyCastExpression.ResultType, castExpression.ResultType);
        }
        public void AddColumn_should_set_properties_and_add_to_columns()
        {
            var table = new EntityType("T", XmlConstants.TargetNamespace_3, DataSpace.SSpace);

            var tableColumn = new EdmProperty(
                "Foo",
                ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(
                    TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))));

            table.AddColumn(tableColumn);

            Assert.NotNull(tableColumn);
            Assert.Equal("Foo", tableColumn.Name);
            Assert.True(table.Properties.Contains(tableColumn));
        }