dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.79k stars 3.19k forks source link

[EFCORE8] ICollection Contains id translate error in sqlserver #32399

Closed xuejmnet closed 12 months ago

xuejmnet commented 12 months ago

    public class MyDbContext:DbContext
    {
        public DbSet<SysUserMod> SysUserMods { get; set; }
        public MyDbContext(DbContextOptions<MyDbContext> options):base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfiguration(new SysUserModMap());
        }
    }

    public class SysUserModMap : IEntityTypeConfiguration<SysUserMod>
    {
        public void Configure(EntityTypeBuilder<SysUserMod> builder)
        {
            builder.HasKey(o => o.Id);
            builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
            builder.Property(o => o.Name).HasMaxLength(128);
            builder.ToTable(nameof(SysUserMod)+"_00");
        }
    }
    public class SysUserMod 
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public int AgeGroup { get; set; }

    }

    internal class Program
    {
        public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
        {
            builder.AddConsole();
        });
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            var build = new DbContextOptionsBuilder<MyDbContext>()
                 .UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;TrustServerCertificate=True;")
                 .UseLoggerFactory(efLogger)
                 .Options;
            var db=new MyDbContext(build);
            //var x = new Object[] { "1", "2" };
            var x = new List<Object> { "1", "2" };
            var u=db.SysUserMods.Where(o => x.Contains(o.Id)).FirstOrDefault();

        }
    }

Include stack traces

Microsoft.Data.SqlClient.SqlException
  HResult=0x80131904
  Message=关键字 'WITH' 附近有语法错误。
关键字 'with' 附近有语法错误。如果此语句是公用表表达式、xmlnamespaces 子句或者更改跟踪上下文子句,那么前一个语句必须以分号结尾。
  Source=Core Microsoft SqlClient Data Provider
  StackTrace:
   在 Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   在 Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   在 Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   在 Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   在 Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   在 Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()
   在 Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
   在 Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   在 Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)
   在 Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   在 Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   在 Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   在 Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) 在 /_/src/EFCore.Relational/Storage/RelationalCommand.cs 中: 第 530 行
   在 Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator) 在 /_/src/EFCore.Relational/Query/Internal/SingleQueryingEnumerable.cs 中: 第 240 行
   在 Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.<>c.<MoveNext>b__21_0(DbContext _, Enumerator enumerator) 在 /_/src/EFCore.Relational/Query/Internal/SingleQueryingEnumerable.cs 中: 第 168 行
   在 Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) 在 /_/src/EFCore.SqlServer/Storage/Internal/SqlServerExecutionStrategy.cs 中: 第 57 行
   在 Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext() 在 /_/src/EFCore.Relational/Query/Internal/SingleQueryingEnumerable.cs 中: 第 231 行
   在 System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
   在 Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) 在 /_/src/EFCore/Query/Internal/QueryCompiler.cs 中: 第 62 行
   在 Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) 在 /_/src/EFCore/Query/Internal/EntityQueryProvider.cs 中: 第 64 行
   在 ConsoleApp7.Program.Main(String[] args) 在 C:\Users\Administrator\Desktop\projects\githubs\ConsoleApp7\ConsoleApp7\Program.cs 中: 第 23 行

Include verbose output

image

Include provider and version information

EF Core version: 8 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 8.0) Operating system: IDE: (e.g. Visual Studio 2022 17.4)

SQLSERVER 2012

ajcvickers commented 12 months ago

@xuejmnet You will need to tell EF your database compatibility level. See: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/breaking-changes#sqlserver-contains-compatibility

xuejmnet commented 12 months ago

@ajcvickers thanks