/// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual bool Apply(InternalModelBuilder modelBuilder, EntityType type)
        {
            if (type.HasDefiningNavigation())
            {
                var entityTypes     = modelBuilder.Metadata.GetEntityTypes(type.Name);
                var otherEntityType = entityTypes.FirstOrDefault();
                if (otherEntityType?.HasDefiningNavigation() == true)
                {
                    var ownership = otherEntityType.FindOwnership();
                    if (ownership != null &&
                        entityTypes.Count == 1)
                    {
                        using (modelBuilder.Metadata.ConventionDispatcher.StartBatch())
                        {
                            var detachedRelationship = InternalEntityTypeBuilder.DetachRelationship(ownership);

                            var weakSnapshot = InternalEntityTypeBuilder.DetachAllMembers(otherEntityType);
                            modelBuilder.RemoveEntityType(otherEntityType, ConfigurationSource.Explicit);

                            detachedRelationship.WeakEntityTypeSnapshot = weakSnapshot;
                            detachedRelationship.Attach(ownership.PrincipalEntityType.Builder);
                        }
                    }
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        private InternalEntityTypeBuilder Entity(
            TypeIdentity type,
            string definingNavigationName,
            EntityType definingEntityType,
            ConfigurationSource configurationSource)
        {
            if (IsIgnored(type, configurationSource))
            {
                return(null);
            }

            var clrType        = type.Type;
            var weakEntityType = clrType == null
                ? Metadata.FindEntityType(type.Name, definingNavigationName, definingEntityType)
                : Metadata.FindEntityType(clrType, definingNavigationName, definingEntityType);

            if (weakEntityType == null)
            {
                var entityType = clrType == null
                    ? Metadata.FindEntityType(type.Name)
                    : Metadata.FindEntityType(clrType);

                IConventionBatch    batch = null;
                EntityType.Snapshot entityTypeSnapshot = null;
                if (entityType != null)
                {
                    if (!configurationSource.Overrides(entityType.GetConfigurationSource()))
                    {
                        return(null);
                    }

                    batch = ModelBuilder.Metadata.ConventionDispatcher.StartBatch();
                    entityTypeSnapshot = InternalEntityTypeBuilder.DetachAllMembers(entityType);

                    Ignore(entityType, configurationSource);
                }

                if (clrType == null)
                {
                    Metadata.Unignore(type.Name);

                    weakEntityType = Metadata.AddEntityType(type.Name, definingNavigationName, definingEntityType, configurationSource);
                }
                else
                {
                    Metadata.Unignore(clrType);

                    weakEntityType = Metadata.AddEntityType(clrType, definingNavigationName, definingEntityType, configurationSource);
                }

                if (batch != null)
                {
                    entityTypeSnapshot.Attach(weakEntityType.Builder);
                    batch.Dispose();
                }
            }
            else
            {
                weakEntityType.UpdateConfigurationSource(configurationSource);
            }

            return(weakEntityType?.Builder);
        }