public void MetadataProtobufSerializationTest()
        {
            var builder = new MetadataModelBuilder();
            MetadataModelBuilder clone = null;

            using (var ctx = CreateContext())
            {
                builder.UseDbContext(ctx);
            }

            var model = builder.ToModel();

            var proto = ProtoBuf.Meta.RuntimeTypeModel.Default.CreateFormatter(typeof(MetadataModelBuilder));

            using (var ms = new MemoryStream())
            {
                proto.Serialize(ms, builder);
                ms.Seek(0, SeekOrigin.Begin);
                clone = proto.Deserialize(ms) as MetadataModelBuilder;
            }

            var cloneModel = clone.ToModel();

            Assert.Equal(model.Entities, cloneModel.Entities, new EntityMetadataComparer());
        }
Example #2
0
        public void MetadataModelBuilderFromModelTest()
        {
            var builder       = new MetadataModelBuilder();
            var entityBuilder = builder.Entity <Receipt>();

            entityBuilder.Property(p => p.Id).ValueGeneration = AutoGenerateValue.OnInsert;
            var nr = entityBuilder.Property(p => p.ReceiptNumber);

            nr.MaxLength = 20;
            nr.Nullable  = false;

            entityBuilder.PrimaryKey.Add("Id");

            var invoice = builder.Entity <Invoice>();

            invoice.BaseEntity = entityBuilder;
            var delivery = invoice.Property(p => p.ExpectedDeliveryDate);

            delivery.DateTimeType = DateTimePropertyType.DateTime;

            var oldModel = builder.ToModel();

            var newBuilder = new MetadataModelBuilder().FromModel(oldModel);
            var newModel   = newBuilder.ToModel();

            Assert.All(oldModel.Entities, p => Assert.NotNull(newModel.GetEntityMetadata(p.ClrType)));
            Assert.All(oldModel.Entities,
                       p =>
            {
                var newEntity = newModel.GetEntityMetadata(p.ClrType);
                Assert.All(p.GetProperties(), x => Assert.NotNull(newEntity.GetProperties().FirstOrDefault(y => y.Name == x.Name)));
            });
        }
Example #3
0
        public void InequalityTest()
        {
            var builder = new MetadataModelBuilder();

            builder.Entity <Invoice>().Property(p => p.ReceiptType);
            builder.Entity <Invoice>().Property(p => p.ReceiptNumber);
            builder.Entity <Invoice>().Property(p => p.Id);
            builder.Entity <Invoice>().Property(p => p.ReceiptDate);
            builder.Entity <Invoice>().PrimaryKey.Add("ReceiptNumber");
            builder.Entity <Invoice>().PrimaryKey.Add("ReceiptType");
            var model = builder.ToModel();

            var entity  = model.GetEntityMetadata <Invoice>();
            var invoice = new Invoice {
                ReceiptNumber = "TestNumber", ReceiptType = ReceiptType.Invoice
            };

            var key   = entity.GetPrimaryKeyObject(invoice);
            var typed = key as EntityKey <string, ReceiptType>;

            var keycompare = new EntityKey <string, ReceiptType>(entity)
            {
                Value0 = "Bla",
                Value1 = ReceiptType.Invoice
            };

            Assert.NotEqual(key.GetHashCode(), keycompare.GetHashCode());
            Assert.NotEqual(key, keycompare);
        }
Example #4
0
        public void FromDbOneToManyMultimplicityTest()
        {
            var builder = new MetadataModelBuilder();

            using (var ctx = new TestContext())
            {
                builder.UseDbContext(ctx);
            }

            var model         = builder.ToModel();
            var receipt       = model.GetEntityMetadata <Receipt>();
            var receiptDetail = model.GetEntityMetadata <ReceiptDetail>();

            var receiptDetailsProperty       = (NavigationPropertyMetadata)receipt["Details"];
            var receiptDetailReceiptProperty = (NavigationPropertyMetadata)receiptDetail["Receipt"];

            Assert.Equal(NavigationPropertyMultiplicity.Many, receiptDetailsProperty.Multiplicity);
            Assert.Equal(NavigationPropertyMultiplicity.One, receiptDetailsProperty.TargetMultiplicity);
            Assert.Equal(NavigationPropertyMultiplicity.One, receiptDetailReceiptProperty.Multiplicity);
            Assert.Equal(NavigationPropertyMultiplicity.Many, receiptDetailReceiptProperty.TargetMultiplicity);

            Assert.Equal(receiptDetailsProperty.TargetNavigationProperty, receiptDetailReceiptProperty);
            Assert.Equal(receiptDetailReceiptProperty.TargetNavigationProperty, receiptDetailsProperty);
            Assert.Single(receiptDetailReceiptProperty.ForeignKeyProperties);
            Assert.Equal(receiptDetail["ReceiptId"], receiptDetailReceiptProperty.ForeignKeyProperties[0].Dependant);
            Assert.Equal(receipt["Id"], receiptDetailReceiptProperty.ForeignKeyProperties[0].Principal);
        }
        public void MetadataDataContractSerializationTest()
        {
            var builder = new MetadataModelBuilder();
            MetadataModelBuilder clone = null;

            using (var ctx = CreateContext())
            {
                builder.UseDbContext(ctx);
            }

            var model = builder.ToModel();

            var dc = new DataContractSerializer(typeof(MetadataModelBuilder));

            using (var ms = new MemoryStream())
            {
                dc.WriteObject(ms, builder);
                ms.Seek(0, SeekOrigin.Begin);
                clone = dc.ReadObject(ms) as MetadataModelBuilder;
            }

            var cloneModel = clone.ToModel();

            Assert.Equal(model.Entities, cloneModel.Entities, new EntityMetadataComparer());
        }
Example #6
0
        public void FromDbAbstractBaseClassRelationTest()
        {
            var builder = new MetadataModelBuilder();

            using (var ctx = new TestContext())
            {
                builder.UseDbContext(ctx);
            }

            var model = builder.ToModel();

            model.GetEntityMetadata <ChangeVersion>();
        }
        public void ProtoBufSerializationPreviousVersionDeserializationTest()
        {
            MetadataModelBuilder builder = null;

            using (var ms = typeof(MetadataModelBuilderSerializationTest).Assembly.GetManifestResourceStream("Lucile.Core.Test.Serialization.previous-model.proto"))
            {
                builder = ProtoBuf.Serializer.Deserialize <MetadataModelBuilder>(ms);
            }

            var model = builder.ToModel();

            TestModelValidations.ValidateInvoiceArticleDefaultModel(model);
        }
Example #8
0
        public void MaxLengthAnnotation_ExpectsValue()
        {
            var builder = new MetadataModelBuilder();

            using (var ctx = new TestContext())
            {
                builder.UseDbContext(ctx);
            }

            var model  = builder.ToModel();
            var entity = model.GetEntityMetadata <ArticleName>();

            Assert.Equal(255, ((TextProperty)entity.GetProperties().First(p => p.Name == nameof(ArticleName.TranlatedText))).MaxLength);
        }
Example #9
0
        public void TestNoneClrPropertiyInModel_UsePropertyMethod_ExpectsOK()
        {
            var modelBuilder = new MetadataModelBuilder();

            modelBuilder.Entity <Contact>()
            .Property <string>("NoneClrProperty");

            var model  = modelBuilder.ToModel();
            var entity = model.GetEntityMetadata <Contact>();

            var property = entity["NoneClrProperty"];

            Assert.NotNull(property);
        }
        public void DataContractSerializationPreviousVersionDeserializationTest()
        {
            MetadataModelBuilder builder = null;

            using (var ms = typeof(MetadataModelBuilderSerializationTest).Assembly.GetManifestResourceStream("Lucile.Core.Test.Serialization.previous-model.xml"))
            {
                var ser = new DataContractSerializer(typeof(MetadataModelBuilder));
                builder = ser.ReadObject(ms) as MetadataModelBuilder;
            }

            var model = builder.ToModel();

            TestModelValidations.ValidateInvoiceArticleDefaultModel(model);
        }
Example #11
0
        public void CheckUniqueNameForGenericEntitiesTest()
        {
            var builder = new MetadataModelBuilder();

            using (var ctx = new TestContext())
            {
                builder.UseDbContext(ctx);
            }

            var model = builder.ToModel();

            foreach (var entity in model.Entities)
            {
                Assert.All(model.Entities.Except(new[] { entity }), p => Assert.NotEqual(entity.Name, p.Name));
            }
        }
Example #12
0
        public void TestNoneClrPropertiyInModel_UsePropertyNameProperty_ExpectsOK()
        {
            var modelBuilder = new MetadataModelBuilder();

            var entityBuilder = modelBuilder.Entity <Contact>();

            entityBuilder.Properties.Add(new TextPropertyBuilder {
                Name = "NoneClrProperty", PropertyType = new ClrTypeInfo(typeof(string))
            });

            var model  = modelBuilder.ToModel();
            var entity = model.GetEntityMetadata <Contact>();

            var property = entity["NoneClrProperty"];

            Assert.NotNull(property);
        }
        public void DefaultValueAnnotationTest()
        {
            var builder = new MetadataModelBuilder();

            using (var ctx = CreateContext())
            {
                builder.UseDbContext(ctx);
            }

            var model = builder.ToModel();

            var prop  = model.GetEntityMetadata <ArticleName>().GetProperties().First(p => p.Name == "TranlatedText");
            var prop2 = model.GetEntityMetadata <ArticleName>().GetProperties().First(p => p.Name == "LanguageId");

            Assert.True(prop.HasDefaultValue);
            Assert.False(prop2.HasDefaultValue);
        }
Example #14
0
        public void EntityMetadataBuilderReplacetItemWithNullTest()
        {
            var builder = new MetadataModelBuilder();

            var receiptBuilder = builder.Entity <Receipt>();

            receiptBuilder.HasMany(p => p.Details)
            .WithOne(p => p.Receipt)
            .HasForeignKey("ReceiptId");
            receiptBuilder.Property(p => p.Id).Nullable = false;
            receiptBuilder.PrimaryKey.Add("Id");

            builder.Entity <Invoice>().BaseEntity = receiptBuilder;

            var receiptDetailBuilder = builder.Entity <ReceiptDetail>();

            receiptDetailBuilder.PrimaryKey.Add("Id");
            receiptDetailBuilder.Property(p => p.Id).Nullable        = false;
            receiptDetailBuilder.Property(p => p.ReceiptId).Nullable = false;

            var model = builder.ToModel();

            var receipt = new Invoice {
                Id = Guid.NewGuid()
            };
            var rd1 = new ReceiptDetail {
                Id = Guid.NewGuid(), ReceiptId = receipt.Id
            };
            var rd2 = new ReceiptDetail {
                Id = Guid.NewGuid(), ReceiptId = receipt.Id
            };

            receipt.Details.AddRange(new[] { rd1, rd2 });

            var entity  = model.GetEntityMetadata(receipt);
            var details = entity.GetNavigations().First(p => p.Name == "Details");

            Assert.Equal(2, receipt.Details.Count);
            Assert.All(receipt.Details, p => Assert.NotNull(p));

            details.ReplaceItem(receipt, rd1, null);

            Assert.Single(receipt.Details);
            Assert.All(receipt.Details, p => Assert.NotNull(p));
        }
Example #15
0
        public void ExcludeQueryTypes_ExpectsOk()
        {
            var builder = new MetadataModelBuilder();

            using (var ctx = new TestContext())
            {
                builder.UseDbContext(ctx);
                var queryType = ctx.Model.FindEntityType(typeof(ArticleStatistics));
                Assert.NotNull(queryType);
                Assert.Null(queryType.FindPrimaryKey());
            }

            var model = builder.ToModel();

            var metadataEntry = model.GetEntityMetadata <ArticleStatistics>();

            Assert.Null(metadataEntry);
        }
Example #16
0
        public void FromDbOneToOneMultimplicityTest()
        {
            var builder = new MetadataModelBuilder();

            using (var ctx = new TestContext())
            {
                builder.UseDbContext(ctx);
            }

            var model   = builder.ToModel();
            var contact = model.GetEntityMetadata <Contact>();

            var contactSettings = model.GetEntityMetadata <ContactSettings>();
            var contactSettingsContactProperty = (NavigationPropertyMetadata)contactSettings["Contact"];

            Assert.Equal(NavigationPropertyMultiplicity.One, contactSettingsContactProperty.Multiplicity);
            Assert.Equal(NavigationPropertyMultiplicity.ZeroOrOne, contactSettingsContactProperty.TargetMultiplicity);
            Assert.Null(contactSettingsContactProperty.TargetNavigationProperty);
            Assert.Equal(contact, contactSettingsContactProperty.TargetEntity);
        }
Example #17
0
        public void GetEntityInheritenceTest()
        {
            var builder = new MetadataModelBuilder();
            var receipt = builder.Entity <Receipt>();
            var invoice = builder.Entity <Invoice>();

            invoice.BaseEntity = receipt;
            var order = builder.Entity <Order>();

            order.BaseEntity = invoice;

            var model = builder.ToModel();

            var invoiceInstance = new Invoice();
            var entity          = model.GetEntityMetadata(invoiceInstance);

            Assert.Equal("Invoice", entity.Name);
            entity = model.GetEntityMetadata <Order>();

            Assert.Equal("Order", entity.Name);
        }
Example #18
0
        private static MetadataModel GetModel()
        {
            var metadataModelBuilder = new MetadataModelBuilder();

            metadataModelBuilder.Exclude <EntityBase>();
            metadataModelBuilder.Entity <ArticleName>().PrimaryKey.AddRange(new[] { "ArticleId", "LanguageId" });
            var articleEntity = metadataModelBuilder.Entity <Article>();

            metadataModelBuilder.Entity <ReceiptDetail>();
            metadataModelBuilder.Entity <Invoice>().BaseEntity = metadataModelBuilder.Entity(typeof(Receipt));
            var contactSettings = metadataModelBuilder.Entity <ContactSettings>();

            articleEntity.HasOne(p => p.ArticleSettings).WithPrincipal();
            contactSettings.HasOne(p => p.Contact).WithDependant();

            metadataModelBuilder.ApplyConventions();

            var model = metadataModelBuilder.ToModel();

            return(model);
        }
        public void JsonSerializationPreviousVersionDeserializationTest()
        {
            MetadataModelBuilder builder = null;

            using (var ms = typeof(MetadataModelBuilderSerializationTest).Assembly.GetManifestResourceStream("Lucile.Core.Test.Serialization.previous-model.json"))
            {
                var json = new JsonSerializerSettings();
                json.Converters.Add(new LucileJsonInheritanceConverter());

                var serializer = JsonSerializer.Create(json);

                using (var streamReader = new StreamReader(ms))
                    using (var jsonReader = new JsonTextReader(streamReader))
                    {
                        builder = serializer.Deserialize <MetadataModelBuilder>(jsonReader);
                    }
            }

            var model = builder.ToModel();

            TestModelValidations.ValidateInvoiceArticleDefaultModel(model);
        }
Example #20
0
        public void GetMetadataPerformanceTest()
        {
            var builder = new MetadataModelBuilder();
            var receipt = builder.Entity <Receipt>();
            var invoice = builder.Entity <Invoice>();
            var order   = builder.Entity <Order>();

            invoice.Property(p => p.Id);
            invoice.PrimaryKey.Add("Id");

            var model = builder.ToModel();

            var inv = new Invoice {
                Id = Guid.NewGuid()
            };
            var ord = new Order {
                Id = Guid.NewGuid()
            };

            var sw = new Stopwatch();

            sw.Start();

            for (int i = 0; i < 2000000; i++)
            {
                if (i % 2 == 0)
                {
                    var entity = model.GetEntityMetadata(inv);
                }
                else
                {
                    var entity = model.GetEntityMetadata(ord);
                }
            }

            sw.Stop();

            Assert.True(sw.Elapsed.TotalSeconds < 1, "too slow");
        }
Example #21
0
        public void IncludeShadowProperties_ExpectsOk()
        {
            var builder = new MetadataModelBuilder();

            using (var ctx = new TestContext())
            {
                builder.UseDbContext(ctx);
                var entityType = ctx.Model.FindEntityType(typeof(Article));
                Assert.NotNull(entityType);
                Assert.NotNull(entityType.FindProperty("CreatedBy"));
#if EF3
                Assert.True(entityType.FindProperty("CreatedBy").IsShadowProperty());
#else
                Assert.True(entityType.FindProperty("CreatedBy").IsShadowProperty);
#endif
            }

            var model = builder.ToModel();

            var metadataEntry = model.GetEntityMetadata <Article>();
            Assert.True(metadataEntry.GetProperties(true).Any(p => p.Name == "CreatedBy"));
        }
Example #22
0
        public void FromDbIdentityAnPrimaryKeyTest()
        {
            var builder = new MetadataModelBuilder();

            using (var ctx = new TestContext())
            {
                builder.UseDbContext(ctx);
            }

            var model = builder.ToModel();

            var country       = model.GetEntityMetadata <Country>();
            var countryIdProp = (ScalarProperty)country["Id"];

            Assert.Equal(1, country.GetProperties().Count(p => p.IsPrimaryKey));
            Assert.Equal(AutoGenerateValue.OnInsert, countryIdProp.ValueGeneration);
            Assert.True(countryIdProp.IsPrimaryKey);

            var contact       = model.GetEntityMetadata <Contact>();
            var contactIdProp = (ScalarProperty)contact["Id"];

            Assert.Equal(1, contact.GetProperties().Count(p => p.IsPrimaryKey));
            Assert.Equal(AutoGenerateValue.None, contactIdProp.ValueGeneration);
            Assert.True(contactIdProp.IsPrimaryKey);

            var contactSettings           = model.GetEntityMetadata <ContactSettings>();
            var contactSettingsIdProperty = (ScalarProperty)contactSettings["Id"];

            Assert.Equal(1, contactSettings.GetProperties().Count(p => p.IsPrimaryKey));
            Assert.Equal(AutoGenerateValue.None, contactSettingsIdProperty.ValueGeneration);
            Assert.True(contactSettingsIdProperty.IsPrimaryKey);

            var invoice           = model.GetEntityMetadata <Invoice>();
            var invoiceIdProperty = (ScalarProperty)invoice["Id"];

            Assert.Equal(1, invoice.GetProperties().Count(p => p.IsPrimaryKey));
            Assert.Equal(AutoGenerateValue.None, invoiceIdProperty.ValueGeneration);
            Assert.True(invoiceIdProperty.IsPrimaryKey);
        }
Example #23
0
        public void MetadataModelSortByDependencyTest()
        {
            var metadataModelBuilder  = new MetadataModelBuilder();
            var articleEntity         = metadataModelBuilder.Entity <Article>();
            var receiptEntity         = metadataModelBuilder.Entity <Receipt>();
            var receiptDetailEntity   = metadataModelBuilder.Entity <ReceiptDetail>();
            var articleSettingsEntity = metadataModelBuilder.Entity <ArticleSettings>();

            articleSettingsEntity.Property(p => p.Id);
            articleSettingsEntity.Property(p => p.Whatever);
            articleSettingsEntity.PrimaryKey.Add("Id");

            articleEntity.Property(p => p.Id);
            articleEntity.Property(p => p.Price);
            articleEntity.PrimaryKey.Add("Id");

            receiptEntity.Property(p => p.Id);
            receiptEntity.Property(p => p.ReceiptNumber);
            receiptEntity.PrimaryKey.Add("Id");

            receiptDetailEntity.Property(p => p.Id);
            receiptDetailEntity.Property(p => p.ReceiptId);
            receiptDetailEntity.Property(p => p.ArticleId);
            receiptDetailEntity.PrimaryKey.Add("Id");

            articleEntity.HasOne(p => p.ArticleSettings).WithPrincipal();
            receiptDetailEntity.HasOne(p => p.Receipt).WithMany(p => p.Details).HasForeignKey("ReceiptId");
            receiptDetailEntity.HasOne(p => p.Article).WithMany().HasForeignKey("ArticleId");

            var model = metadataModelBuilder.ToModel();

            var sorted = model.SortedByDependency();

            Assert.Contains(sorted.Take(2), p => p.Name == "Article");
            Assert.Contains(sorted.Take(2), p => p.Name == "Receipt");
            Assert.Contains(sorted.Skip(2), p => p.Name == "ArticleSettings");
            Assert.Contains(sorted.Skip(2), p => p.Name == "ReceiptDetail");
        }
Example #24
0
        public void CheckEnumProperties()
        {
            var builder = new MetadataModelBuilder();

            using (var ctx = new TestContext())
            {
                builder.UseDbContext(ctx);
            }

            var model = builder.ToModel();

            var enumProperty         = model.GetEntityMetadata <AllTypesEntity>().GetProperties().First(p => p.Name == nameof(AllTypesEntity.EnumProperty));
            var nullableEnumProperty = model.GetEntityMetadata <AllTypesEntity>().GetProperties().First(p => p.Name == nameof(AllTypesEntity.NullableEnumProperty));

            Assert.IsType <EnumProperty>(enumProperty);
            Assert.IsType <EnumProperty>(nullableEnumProperty);

            Assert.False(enumProperty.Nullable);
            Assert.True(nullableEnumProperty.Nullable);

            Assert.Equal(NumericPropertyType.Byte, ((EnumProperty)enumProperty).UnderlyingNumericType);
            Assert.Equal(NumericPropertyType.Byte, ((EnumProperty)nullableEnumProperty).UnderlyingNumericType);
        }
Example #25
0
        private void BuildModel(out List <PropertyConfiguration> propConfigs, out List <SourceEntityConfiguration> sourceEntityConfigs, out Data.Metadata.EntityMetadata entity)
        {
            var entityModelBuilder = new MetadataModelBuilder();
            var entityBuilder      = entityModelBuilder.Entity <TResult>();

            propConfigs         = new List <PropertyConfiguration>();
            sourceEntityConfigs = _sourceBuilders.Select(p => p.Value.ToTarget()).ToList();
            foreach (var item in _propertyBuilders)
            {
                var expression = item.Value.MappedExpression;

                if (expression.Body.NodeType == ExpressionType.New || expression.Body.NodeType == ExpressionType.MemberInit)
                {
                    var nav = entityBuilder.Navigation(item.Value.PropertyName);
                }
                else
                {
                    entityBuilder.Property(item.Value.PropertyName, item.Value.PropertyType);
                    if (item.Value.IsPrimaryKey)
                    {
                        entityBuilder.PrimaryKey.Add(item.Value.PropertyName);
                    }
                }

                // TODO: Merge Metadata from mapped Properties
            }

            var model = entityModelBuilder.ToModel();

            entity = model.GetEntityMetadata <TResult>();
            foreach (var item in _propertyBuilders)
            {
                var prop   = entity[item.Value.PropertyName];
                var config = new PropertyConfiguration(prop, item.Value.Label, item.Value.CanAggregate, item.Value.CanFilter, item.Value.CanSort, item.Value.CustomFilterExpression, item.Value.MappedExpression, !IsSingleSourceQuery);
                propConfigs.Add(config);
            }
        }
Example #26
0
        public void GetTargetNavigationPropertyPerformance()
        {
            var builder = new MetadataModelBuilder();

            var receiptBuilder = builder.Entity <Receipt>();

            receiptBuilder.HasMany(p => p.Details)
            .WithOne(p => p.Receipt)
            .HasForeignKey("ReceiptId");
            receiptBuilder.Property(p => p.Id).Nullable = false;
            receiptBuilder.PrimaryKey.Add("Id");

            builder.Entity <Invoice>().BaseEntity = receiptBuilder;

            var receiptDetailBuilder = builder.Entity <ReceiptDetail>();

            receiptDetailBuilder.PrimaryKey.Add("Id");
            receiptDetailBuilder.Property(p => p.Id).Nullable        = false;
            receiptDetailBuilder.Property(p => p.ReceiptId).Nullable = false;

            var model = builder.ToModel();

            var receipt = model.GetEntityMetadata <Receipt>();
            var details = receipt.GetNavigations().First(p => p.Name == "Details");

            var sw = new Stopwatch();

            sw.Start();
            for (int i = 0; i < 2000000; i++)
            {
                var targetProp = details.TargetNavigationProperty;
            }
            sw.Stop();

            Assert.True(sw.Elapsed.TotalSeconds < 1);
        }
Example #27
0
        public static void ValidateInvoiceArticleDefaultModel(MetadataModelBuilder builder)
        {
            var model = builder.ToModel();

            ValidateInvoiceArticleDefaultModel(model);
        }
Example #28
0
        public void AutomaticFixupTest()
        {
            var metadataModelBuilder = new MetadataModelBuilder();

            metadataModelBuilder.Entity <ArticleName>().PrimaryKey.AddRange(new[] { "ArticleId", "LanguageId" });
            var articleEntity = metadataModelBuilder.Entity <Article>();

            metadataModelBuilder.Entity <ReceiptDetail>();
            metadataModelBuilder.Entity <Invoice>().BaseEntity = metadataModelBuilder.Entity(typeof(Receipt));
            var contactSettings = metadataModelBuilder.Entity <ContactSettings>();

            articleEntity.HasOne(p => p.ArticleSettings).WithPrincipal();
            contactSettings.HasOne(p => p.Contact).WithDependant();

            metadataModelBuilder.ApplyConventions();

            var invoice = new Invoice {
                Id = Guid.NewGuid()
            };
            var article = new Article {
                Id = Guid.NewGuid(), ArticleNumber = "12314", SupplierId = Guid.NewGuid(), ArticleDescription = "testchen"
            };

            var detail = new ReceiptDetail {
                Id = Guid.NewGuid(), Receipt = invoice, ReceiptId = invoice.Id, Amount = 123, ArticleId = article.Id
            };

            invoice.Details.Add(detail);

            var name1 = new ArticleName {
                ArticleId = article.Id, LanguageId = "de", TranlatedText = "Text 1"
            };
            var name2 = new ArticleName {
                ArticleId = article.Id, LanguageId = "en", TranlatedText = "Text 1"
            };

            var context = new ModelContext(metadataModelBuilder.ToModel());

            context.Attach(new[] { name1, name2 });
            context.AttachSingle(article);

            Assert.Equal(article, name1.Article);
            Assert.Equal(article, name2.Article);
            Assert.Equal(2, article.Names.Count);
            Assert.Contains(article.Names, p => p == name1);
            Assert.Contains(article.Names, p => p == name2);

            context.AttachSingle(invoice);

            Assert.Equal(article, detail.Article);

            Assert.Null(article.ArticleSettings);

            var articleSettings = new ArticleSettings {
                Id = article.Id, Whatever = "test"
            };

            context.AttachSingle(articleSettings);

            Assert.Equal(article.ArticleSettings, articleSettings);
        }