question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'Sequence contains no matching element' scaffolding database tables

See original GitHub issue

Steps to reproduce

dotnet ef dbcontext scaffold "connect string" "Pomelo.EntityFrameworkCore.MySql" --verbose

The issue

Throws an exception after not finding design-time services.

Using project 'd:\Client\olab\devel\repos\OLab4\OLab4-api\Service\OLabWebAPI.csproj'.
Using startup project 'd:\Client\olab\devel\repos\OLab4\OLab4-api\Service\OLabWebAPI.csproj'.
Writing 'd:\Client\olab\devel\repos\OLab4\OLab4-api\Service\obj\OLabWebAPI.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\corey\AppData\Local\Temp\tmp3401.tmp /verbosity:quiet /nologo d:\Client\olab\devel\repos\OLab4\OLab4-api\Service\OLabWebAPI.csproj
Writing 'd:\Client\olab\devel\repos\OLab4\OLab4-api\Service\obj\OLabWebAPI.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\corey\AppData\Local\Temp\tmp35D7.tmp /verbosity:quiet /nologo d:\Client\olab\devel\repos\OLab4\OLab4-api\Service\OLabWebAPI.csproj
Build started...
dotnet build d:\Client\olab\devel\repos\OLab4\OLab4-api\Service\OLabWebAPI.csproj /verbosity:quiet /nologo

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.93
Build succeeded.
dotnet exec --depsfile d:\Client\olab\devel\repos\OLab4\OLab4-api\Service\bin\Debug\netcoreapp3.1\OLabWebAPI.deps.json --additionalprobingpath C:\Users\corey\.nuget\packages --runtimeconfig d:\Client\olab\devel\repos\OLab4\OLab4-api\Service\bin\Debug\netcoreapp3.1\OLabWebAPI.runtimeconfig.json C:\Users\corey\.dotnet\tools\.store\dotnet-ef\5.0.0\dotnet-ef\5.0.0\tools\netcoreapp3.1\any\tools\netcoreapp2.0\any\ef.dll dbcontext scaffold server=diskstation.local;uid=entrada_dev;pwd=b012db9d53B!;database=dev_olab Pomelo.EntityFrameworkCore.MySql --assembly d:\Client\olab\devel\repos\OLab4\OLab4-api\Service\bin\Debug\netcoreapp3.1\OLabWebAPI.dll --startup-assembly d:\Client\olab\devel\repos\OLab4\OLab4-api\Service\bin\Debug\netcoreapp3.1\OLabWebAPI.dll --project-dir d:\Client\olab\devel\repos\OLab4\OLab4-api\Service\ --language C# --working-dir d:\Client\olab\devel\repos\OLab4\OLab4-api\Service --verbose --root-namespace OLabWebAPI
Using assembly 'OLabWebAPI'.
Using startup assembly 'OLabWebAPI'.
Using application base 'd:\Client\olab\devel\repos\OLab4\OLab4-api\Service\bin\Debug\netcoreapp3.1'.
Using working directory 'd:\Client\olab\devel\repos\OLab4\OLab4-api\Service'.
Using root namespace 'OLabWebAPI'.
Using project directory 'd:\Client\olab\devel\repos\OLab4\OLab4-api\Service\'.
Remaining arguments: .
Finding design-time services for provider 'Pomelo.EntityFrameworkCore.MySql'...
Using design-time services from provider 'Pomelo.EntityFrameworkCore.MySql'.
Finding design-time services referenced by assembly 'OLabWebAPI'.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'OLabWebAPI'...
No design-time services were found.
System.InvalidOperationException: Sequence contains no matching element
   at System.Linq.ThrowHelper.ThrowNoMatchException()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
   at Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal.MySqlDatabaseModelFactory.GetConstraints(DbConnection connection, IReadOnlyList`1 tables)
   at Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal.MySqlDatabaseModelFactory.GetTables(DbConnection connection, Func`3 filter)
   at Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal.MySqlDatabaseModelFactory.Create(DbConnection connection, DatabaseModelFactoryOptions options)
   at Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal.MySqlDatabaseModelFactory.Create(String connectionString, DatabaseModelFactoryOptions options)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Sequence contains no matching element

Further technical details

MySQL version: 10.3.29 MariaDB Operating system: Windows 10 Pomelo.EntityFrameworkCore.MySql version: 3.2.7

Other details about my project setup:

Solution made up of main ‘Services’ project, with a dependancy to a ‘Data’ classlib project that contains the DBContext.

Ran scaffolding in main .EXE project source directory (OLabWebAPI.csproj). DBContext is in dependant project called ‘Data’. Tried running the scaffolding in Data.csproj directory and same result.

I tried adding the following to the project that has my DBContext, to no avail:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

// this is only used for scaffolding
public class DesignTimeOLabDBContext  : IDesignTimeDbContextFactory<OLabDBContext>
{
  public OLabDBContext CreateDbContext(string[] args)
  {
    var optionsBuilder = new DbContextOptionsBuilder<OLabDBContext>();
    // pass your design time connection string here
    optionsBuilder.UseMySql("connect string");
    return new OLabDBContext(optionsBuilder.Options);
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
lauxjpncommented, Dec 10, 2021

@RotateAt60MPH The No design-time services were found. message is just an informational message that your project did not explicitly define any design-time services. Getting this message with the --verbose flag is normal, as most projects do not explicitly implement their own design-time services. So the message is completely unrelated to your issue.


However, to easily debug the issue (or even implement a workaround later), you can actually just add a custom design time service. Add the CustomMySqlDesignTimeServices and CustomMySqlDatabaseModelFactory classes from the following code to your project:

Program.cs
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Pomelo.EntityFrameworkCore.MySql.Design.Internal;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal;
using Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal;

namespace IssueConsoleTemplate
{
    public class CustomMySqlDesignTimeServices : IDesignTimeServices
    {
        private readonly MySqlDesignTimeServices _originalMySqlDesignTimeServices;

        public CustomMySqlDesignTimeServices()
        {
            _originalMySqlDesignTimeServices = new MySqlDesignTimeServices();
        }

        public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
        {
            _originalMySqlDesignTimeServices.ConfigureDesignTimeServices(serviceCollection);

            serviceCollection.AddScoped<IDatabaseModelFactory, CustomMySqlDatabaseModelFactory>();
        }
    }

    public class CustomMySqlDatabaseModelFactory : MySqlDatabaseModelFactory
    {
        private readonly IDiagnosticsLogger<DbLoggerCategory.Scaffolding> _logger;
        
        public CustomMySqlDatabaseModelFactory(
            IDiagnosticsLogger<DbLoggerCategory.Scaffolding> logger,
            IRelationalTypeMappingSource typeMappingSource,
            IMySqlOptions options)
            : base(
                logger,
                typeMappingSource,
                options)
        {
            _logger = logger;
        }
        
        private const string GetConstraintsQuery = @"SELECT
 	`CONSTRAINT_NAME`,
 	`TABLE_NAME`,
 	`REFERENCED_TABLE_NAME`,
 	GROUP_CONCAT(CONCAT_WS('|', `COLUMN_NAME`, `REFERENCED_COLUMN_NAME`) ORDER BY `ORDINAL_POSITION` SEPARATOR ',') AS PAIRED_COLUMNS,
 	(SELECT `DELETE_RULE` FROM `INFORMATION_SCHEMA`.`REFERENTIAL_CONSTRAINTS` WHERE `REFERENTIAL_CONSTRAINTS`.`CONSTRAINT_NAME` = `KEY_COLUMN_USAGE`.`CONSTRAINT_NAME` AND `REFERENTIAL_CONSTRAINTS`.`CONSTRAINT_SCHEMA` = `KEY_COLUMN_USAGE`.`CONSTRAINT_SCHEMA`) AS `DELETE_RULE`
 FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE`
 WHERE `TABLE_SCHEMA` = '{0}'
 		AND `TABLE_NAME` = '{1}'
 		AND `CONSTRAINT_NAME` <> 'PRIMARY'
        AND `REFERENCED_TABLE_NAME` IS NOT NULL
        GROUP BY `CONSTRAINT_SCHEMA`,
        `CONSTRAINT_NAME`,
        `TABLE_NAME`,
        `REFERENCED_TABLE_NAME`;";

        protected virtual void GetConstraints(
            DbConnection connection,
            IReadOnlyList<DatabaseTable> tables)
        {
            Debugger.Launch(); // <-- launch the debugger

            foreach (var table in tables)
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = string.Format(GetConstraintsQuery, connection.Database, table.Name);
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var referencedTableName = reader.GetString(2);
                            var referencedTable = tables.FirstOrDefault(t => t.Name == referencedTableName);
                            if (referencedTable == null)
                            {
                                // On operation systems with insensitive file name handling, the saved reference table name might have a
                                // different casing than the actual table name. (#1017)
                                // In the unlikely event that there are multiple tables with the same spelling, differing only in casing,
                                // we can't be certain which is the right match, so rather fail to be safe.
                                referencedTable = tables.Single(t => string.Equals(t.Name, referencedTableName, StringComparison.OrdinalIgnoreCase));
                            }
                            if (referencedTable != null)
                            {
                                var fkInfo = new DatabaseForeignKey {Name = reader.GetString(0), OnDelete = ConvertToReferentialAction(reader.GetString(4)), Table = table, PrincipalTable = referencedTable};
                                foreach (var pair in reader.GetString(3).Split(','))
                                {
                                    fkInfo.Columns.Add(table.Columns.Single(y =>
                                        string.Equals(y.Name, pair.Split('|')[0], StringComparison.OrdinalIgnoreCase)));
                                    fkInfo.PrincipalColumns.Add(fkInfo.PrincipalTable.Columns.Single(y =>
                                        string.Equals(y.Name, pair.Split('|')[1], StringComparison.OrdinalIgnoreCase)));
                                }

                                table.ForeignKeys.Add(fkInfo);
                            }
                            else
                            {
                                _logger.Logger.LogWarning($"Referenced table `{referencedTableName}` is not in dictionary.");
                            }
                        }
                    }
                }
            }
        }
    }

    internal static class Program
    {
        private static void Main()
        {
        }
    }
}

The CustomMySqlDatabaseModelFactory.GetConstraints() method is the one that the original 3.2.7 implementation uses (with the Debugger.Launch(); line added).

Just scaffold again and the JIT debugger should pop up. You can then just debug the method (or replace tables.Single() with tables.SingleOrDefault()).

1reaction
mguinnesscommented, Dec 9, 2021

Probably related to PR https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/pull/1364 that hasn’t been backported to 3.2-maint branch yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequence contains no matching element scaffolding visual ...
The model were generated successifully from the database but adding a controller for the specified model using scaffolding returns error.
Read more >
Sequence contains no matching element when scaffolding
Run the scaffold command. dotnet ef dbcontext scaffold "User=SYSDBA;Password=.....;Database=/var/lib/firebird/2.5/data ...
Read more >
Sequence Contains No Matching Element: Solved
Sequence contains no matching element because your search criterion is wrong. Read this article, and we'll teach you how to fix the exception...
Read more >
Solved: Re: Error: Sequence contains no matching element
Hi, I got this same error today and I tried refreshing table by table in the model - so identified which table was...
Read more >
Scaffolding Or Reverse Engineering - Database
ODP.NET EF Core supports scaffolding the following tables and views: Relational tables and views. Materialized views. By convention, ODP.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found