Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does Json Column support a collection value? #29735

Closed
rongconzx opened this issue Dec 2, 2022 · 2 comments
Closed

Does Json Column support a collection value? #29735

rongconzx opened this issue Dec 2, 2022 · 2 comments

Comments

@rongconzx
Copy link

rongconzx commented Dec 2, 2022

Ask a question

I have the column FollowSOIds with the typed string as JSON array int. I mapped it to Json Column in the database context. I got an error in the query could not be translated.
Is there any way to map collection values (int, string) with Json Array in Entity Framework 7?

Include your code

My db scheme

image

 public class UserNotificationPreference
    {
        public int Id { get; set; }
   
        public List<int> FollowSOIds { get; set; }

    }

      public class AppDBContext : DbContext
    {
        public AppDBContext()
            : base()
        {
        }     

    
        public DbSet<UserNotificationPreference> UserNotificationPreference { get; set; }
        protected override void OnModelCreating(ModelBuilder builder)
        {     
            builder.Entity<UserNotificationPreference>()
                .ToTable("UserNotificationPreference").OwnsOne(x => x.FollowSOIds, z => z.ToJson());
        }
}

Include stack traces

image


### Include verbose output


System.InvalidOperationException
HResult=0x80131509
Message=The LINQ expression 'DbSet()
.Where(u => u.FollowSOIds
.Contains(1))' could not be translated. Additional information: Translation of method 'System.Linq.Enumerable.Contains' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Source=Microsoft.EntityFrameworkCore
StackTrace:
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.g__CheckTranslated|15_0(ShapedQueryExpression translated, <>c__DisplayClass15_0& )
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_01.<Execute>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
at Program.

$(String[] args) in C:\Users\admin\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 8


### Include provider and version information

EF Core version: 7
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 7.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.4)
@maumar
Copy link
Contributor

maumar commented Dec 2, 2022

dupe of #29427

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Dec 5, 2022
@roji
Copy link
Member

roji commented Dec 5, 2022

Duplicate of #29427

@roji roji marked this as a duplicate of #29427 Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants