public void Throws_when_adding_a_function_returning_an_existing_owned_type()
        {
            var modelBuilder = CreateModelBuilder();

            modelBuilder.Entity <TestEntity>().OwnsOne(e => e.KeylessEntity);
            modelBuilder.HasDbFunction(typeof(TableValuedDbFunctionConventionTest).GetMethod(
                                           nameof(GetKeylessEntities),
                                           BindingFlags.NonPublic | BindingFlags.Static));

            Assert.Equal(
                RelationalStrings.DbFunctionInvalidIQueryableOwnedReturnType(
                    nameof(GetKeylessEntities), typeof(IQueryable <KeylessEntity>).ShortDisplayName()),
                Assert.Throws <InvalidOperationException>(() => modelBuilder.FinalizeModel()).Message);
        }
Exemplo n.º 2
0
        public void Throws_when_adding_a_function_returning_an_owned_type()
        {
            var modelBuilder = CreateModelBuilder();

            modelBuilder.Owned <KeylessEntity>();
            modelBuilder.HasDbFunction(typeof(TableValuedDbFunctionConventionTest).GetMethod(
                                           nameof(GetKeylessEntities),
                                           BindingFlags.NonPublic | BindingFlags.Static));

            Assert.Equal(
                RelationalStrings.DbFunctionInvalidIQueryableOwnedReturnType(
                    "Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.TableValuedDbFunctionConventionTest.GetKeylessEntities(System.Int32)",
                    typeof(KeylessEntity).ShortDisplayName()),
                Assert.Throws <InvalidOperationException>(() => modelBuilder.FinalizeModel()).Message);
        }
        /// <summary>
        ///     Called when an <see cref="IConventionDbFunction" /> is added to the model.
        /// </summary>
        /// <param name="dbFunctionBuilder"> The builder for the <see cref="IConventionDbFunction" />. </param>
        /// <param name="context"> Additional information associated with convention execution. </param>
        private void ProcessDbFunctionAdded(
            [NotNull] IConventionDbFunctionBuilder dbFunctionBuilder, [NotNull] IConventionContext context)
        {
            var function = dbFunctionBuilder.Metadata;

            if (!function.IsQueryable)
            {
                return;
            }

            var elementType = function.ReturnType.TryGetElementType(typeof(IQueryable <>));

            if (!elementType.IsValidEntityType())
            {
                throw new InvalidOperationException(RelationalStrings.DbFunctionInvalidIQueryableReturnType(
                                                        function.Name, function.ReturnType.ShortDisplayName()));
            }

            var model = function.Model;
            IConventionEntityTypeBuilder entityTypeBuilder;
            var entityType = model.FindEntityType(elementType);

            if (entityType?.IsOwned() == true || model.IsOwned(elementType))
            {
                throw new InvalidOperationException(RelationalStrings.DbFunctionInvalidIQueryableOwnedReturnType(
                                                        function.Name, function.ReturnType.ShortDisplayName()));
            }

            if (entityType != null)
            {
                entityTypeBuilder = entityType.Builder;
            }
            else
            {
                entityTypeBuilder = dbFunctionBuilder.ModelBuilder.Entity(elementType);
                if (entityTypeBuilder == null)
                {
                    return;
                }
            }

            entityTypeBuilder.ToTable(null);
            entityTypeBuilder.HasNoKey();
        }