Example #1
0
        public void RandomTest()
        {
            using (var mysql = ApplicationDbContext.UseMySql())
            {
                var    query = mysql.SimpleModels.Random(2);
                var    sql   = query.ToSql();
                string expectedSql;

                if (EFVersion.AtLeast(3, 0))
                {
                    expectedSql = @"SELECT `s`.`Id`, `s`.`Age`, `s`.`Birthday`, `s`.`Name`, `s`.`State`
FROM `SimpleModels` AS `s`
ORDER BY RAND()
LIMIT @__p_0;
";
                }
                else if (EFVersion.AtLeast(2, 0))
                {
                    expectedSql = @"SELECT `x`.`Id`, `x`.`Age`, `x`.`Birthday`, `x`.`Name`, `x`.`State`
FROM `SimpleModels` AS `x`
ORDER BY RAND()
LIMIT 2;
";
                }
                else
                {
                    throw EFVersion.NotSupportedException;
                }

                Assert.Equal(expectedSql, sql);
            }
        }
Example #2
0
        /// <summary>
        /// Gets the generated Sql string.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="this"></param>
        /// <returns></returns>
        public static string ToSql <TEntity>(this IQueryable <TEntity> @this)
        {
            if (EFVersion.AtLeast(3, 1))
            {
                throw EFVersion.NeedNewerVersionException;
            }
            else if (EFVersion.AtLeast(3, 0))
            {
                var enumerable           = @this.Provider.Execute <IEnumerable <TEntity> >(@this.Expression);
                var reflector_enumerator = enumerable.GetEnumerator().GetReflector();

                var factory      = reflector_enumerator.DeclaredField <IQuerySqlGeneratorFactory>("_querySqlGeneratorFactory").Value;
                var sqlGenerator = factory.Create();

                var selectExpression = reflector_enumerator.DeclaredField <SelectExpression>("_selectExpression").Value;
                var command          = sqlGenerator.GetCommand(selectExpression);
                var sql = $"{command.CommandText};{Environment.NewLine}";

                return(sql);
            }
            else
            {
                throw EFVersion.NotSupportedException;
            }
        }
        private void SetInitialMultiplicity(BidirectionalAssociation element)
        {
            // valid bidirectional associations:
            // EF6 - entity to entity
            // EFCore - entity to entity, entity to dependent,  dependent to entity
            // EFCore5Plus - entity to entity, entity to dependent, dependent to entity

            ModelRoot modelRoot = element.Source.ModelRoot;
            EFVersion entityFrameworkVersion = modelRoot.EntityFrameworkVersion;

            if (entityFrameworkVersion == EFVersion.EF6)
            {
                if (element.Source.IsEntity() && element.Target.IsEntity())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.ZeroMany;
                }
            }
            else if (entityFrameworkVersion == EFVersion.EFCore && !modelRoot.IsEFCore5Plus)
            {
                if (element.Source.IsEntity() && element.Target.IsEntity())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.ZeroMany;
                }

                if (element.Source.IsEntity() && element.Target.IsDependent())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.One;
                }

                if (element.Source.IsDependent() && element.Target.IsEntity())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.One;
                }
            }
            else if (entityFrameworkVersion == EFVersion.EFCore && modelRoot.IsEFCore5Plus)
            {
                if (element.Source.IsEntity() && element.Target.IsEntity())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.ZeroMany;
                }

                if (element.Source.IsEntity() && element.Target.IsDependent())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.One;
                }

                if (element.Source.IsDependent() && element.Target.IsEntity())
                {
                    element.SourceMultiplicity = Multiplicity.One;
                    element.TargetMultiplicity = Multiplicity.One;
                }
            }
        }
Example #4
0
 public NuGetDisplay(EFVersion efVersion, string packageId, string packageVersion, string display, string majorMinorVersion)
 {
     EFVersion            = efVersion;
     PackageId            = packageId;
     ActualPackageVersion = packageVersion;
     DisplayVersion       = display;
     MajorMinorVersion    = majorMinorVersion;
 }
Example #5
0
        private static bool CanAcceptModelClassAndModelClassAsSourceAndTarget(ModelClass sourceModelClass, ModelClass targetModelClass)
        {
            // valid bidirectional associations:
            // EF6 - entity to entity
            // EFCore - entity to entity, entity to dependent,  dependent to entity
            // EFCore5Plus - entity to entity, entity to dependent, dependent to entity

            ModelRoot modelRoot = sourceModelClass.ModelRoot;
            EFVersion entityFrameworkVersion = modelRoot.EntityFrameworkVersion;

            if (entityFrameworkVersion == EFVersion.EF6)
            {
                if (sourceModelClass.IsEntity() && targetModelClass.IsEntity())
                {
                    return(true);
                }
            }
            else if (entityFrameworkVersion == EFVersion.EFCore && !modelRoot.IsEFCore5Plus)
            {
                if (sourceModelClass.IsEntity() && targetModelClass.IsEntity())
                {
                    return(true);
                }

                if (sourceModelClass.IsEntity() && targetModelClass.IsDependent())
                {
                    return(true);
                }

                if (sourceModelClass.IsDependent() && targetModelClass.IsEntity())
                {
                    return(true);
                }
            }
            else if (entityFrameworkVersion == EFVersion.EFCore && modelRoot.IsEFCore5Plus)
            {
                if (sourceModelClass.IsEntity() && targetModelClass.IsEntity())
                {
                    return(true);
                }

                if (sourceModelClass.IsEntity() && targetModelClass.IsDependent())
                {
                    return(true);
                }

                if (sourceModelClass.IsDependent() && targetModelClass.IsEntity())
                {
                    return(true);
                }
            }

            return(false);
        }
        private static bool CanAcceptModelClassAsTarget(ModelClass candidate)
        {
            // valid unidirectional associations:
            // EF6 - entity to entity, entity to dependent
            // EFCore - entity to entity, entity to dependent
            // EFCore5Plus - entity to entity, entity to dependent, dependent to dependent, keyless to entity

            ModelRoot modelRoot = candidate.ModelRoot;
            EFVersion entityFrameworkVersion = modelRoot.EntityFrameworkVersion;

            if (entityFrameworkVersion == EFVersion.EF6)
            {
                if (candidate.IsEntity())
                {
                    return(true);
                }

                if (candidate.IsDependent())
                {
                    return(true);
                }
            }
            else if (entityFrameworkVersion == EFVersion.EFCore && !modelRoot.IsEFCore5Plus)
            {
                if (candidate.IsEntity())
                {
                    return(true);
                }

                if (candidate.IsDependent())
                {
                    return(true);
                }
            }
            else if (entityFrameworkVersion == EFVersion.EFCore && modelRoot.IsEFCore5Plus)
            {
                if (candidate.IsEntity())
                {
                    return(true);
                }

                if (candidate.IsDependent())
                {
                    return(true);
                }
            }

            return(false);
        }
Example #7
0
        /// <summary>
        /// Gets the generated Sql string.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="this"></param>
        /// <returns></returns>
        public static string ToSql <TEntity>(this IQueryable <TEntity> @this)
        {
            if (EFVersion.AtLeast(3, 0))
            {
                throw EFVersion.NeedNewerVersionException;
            }
            if (EFVersion.AtLeast(2, 1))
            {
                var queryCompiler          = @this.Provider.GetReflector <EntityQueryProvider>().DeclaredField <QueryCompiler>("_queryCompiler").Value;
                var queryCompilerReflector = queryCompiler.GetReflector();

                var queryModel = queryCompilerReflector.DeclaredField <QueryModelGenerator>("_queryModelGenerator").Value.ParseQuery(@this.Expression);

                var dependencies = queryCompilerReflector.DeclaredProperty <Database>("Database").DeclaredProperty <DatabaseDependencies>("Dependencies").Value;
                var modelVisitor = dependencies.QueryCompilationContextFactory.Create(false).CreateQueryModelVisitor() as RelationalQueryModelVisitor;
                modelVisitor.CreateQueryExecutor <TEntity>(queryModel);

                var sql = modelVisitor.Queries.Select(x => $"{x.ToString().TrimEnd(';')};{Environment.NewLine}").Join("");
                return(sql);
            }
            else if (EFVersion.AtLeast(2, 0))
            {
                var queryCompiler          = @this.Provider.GetReflector <EntityQueryProvider>().DeclaredField <QueryCompiler>("_queryCompiler").Value;
                var queryCompilerReflector = queryCompiler.GetReflector();

                var nodeTypeProvider = queryCompilerReflector.DeclaredProperty <INodeTypeProvider>("NodeTypeProvider").Value;
                var parser           = queryCompilerReflector.Method("CreateQueryParser").Call(nodeTypeProvider) as QueryParser;
                var queryModel       = parser.GetParsedQuery(@this.Expression);

                var dependencies = queryCompilerReflector.DeclaredProperty <Database>("Database").DeclaredProperty <DatabaseDependencies>("Dependencies").Value;
                var modelVisitor = dependencies.QueryCompilationContextFactory.Create(false).CreateQueryModelVisitor() as RelationalQueryModelVisitor;
                modelVisitor.CreateQueryExecutor <TEntity>(queryModel);

                var sql = modelVisitor.Queries.Select(x => $"{x.ToString().TrimEnd(';')};{Environment.NewLine}").Join("");
                return(sql);
            }
            else
            {
                throw EFVersion.NotSupportedException;
            }
        }
Example #8
0
        private static void LoadNuGetVersions(EFVersion efVersion, string packageId)
        {
            // get NuGet packages with that package id
            HttpResponseMessage responseMessage = httpClient.GetAsync(string.Format(NUGET_URL, packageId)).GetAwaiter().GetResult();
            string jsonString = responseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult();

            NuGetPackages nugetPackages = NuGetPackages.FromJson(jsonString);
            string        id            = packageId.ToLower();

            // get their versions
            List <string> result = nugetPackages.Data
                                   .Where(x => x.Title.ToLower() == id)
                                   .SelectMany(x => x.Versions)
                                   .OrderBy(v => v.VersionVersion)
                                   .Select(v => v.VersionVersion)
                                   .ToList();

            // find the major.minor versions
            List <string> majorVersions = result.Select(v => string.Join(".", v.Split('.').Take(2))).OrderBy(v => v).Distinct().ToList();

            // do the trivial mapping of the full version to the full display name
            foreach (string v in result)
            {
                NuGetPackageDisplay.Add(new NuGetDisplay(efVersion, packageId, v, v, string.Join(".", v.Split('.').Take(2))));
            }

            // figure out which one is the latest in the major.minor set and add its mapping
            foreach (string v in majorVersions)
            {
                NuGetPackageDisplay.Add(new NuGetDisplay(efVersion, packageId, result.FindLast(x => x.StartsWith($"{v}.")), $"{v}.Latest", v));
            }

            // figure out which is the overall latest and map it
            NuGetPackageDisplay.Add(new NuGetDisplay(efVersion, packageId, result.FindLast(x => !x.EndsWith(".Latest")), "Latest", majorVersions.Last()));

            // tuck it away
            EFPackageVersions.Add(efVersion, NuGetPackageDisplay.Where(p => p.EFVersion == efVersion).Select(p => p.DisplayVersion).ToList());
        }