-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Navigation in owned JSON entity #32137
Comments
It seems like you're asking for an owned JSON entity (B) to have a navigation out to an entity outside the document (C), which is mapped to its own table. This currently isn't supported. As a workaround, you can store the Id of C inside B directly as a foreign key (i.e. as a long), and then use LINQ Join to fetch C in the same query. |
1, Will that be supported in the future? |
That would be great if you're willing! |
Sorry for taking so long to reply with a sample. What you're trying to achieve should be doable via the following: _ = await ctx.A
.Select(a => new
{
A = a,
Definitions = ctx.C
.Where(c => a.Items.Select(b => b.DefinitionId).Contains(c.Id))
.ToList()
})
.ToListAsync(); In other words, for each A, query out the rows from C whose Id is in the list of definition IDs from inside A's B. Full code sampleawait using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
_ = await ctx.A
.Select(a => new
{
A = a,
Definitions = ctx.C
.Where(c => a.Items.Select(b => b.DefinitionId).Contains(c.Id))
.ToList()
})
.ToListAsync();
public class BlogContext : DbContext
{
public DbSet<A> A { get; set; }
public DbSet<C> C { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<A>()
.OwnsMany(a => a.Items, a => a.ToJson());
}
}
public class A // has table in db
{
public long Id { get; set; }
public List<B> Items { get; set; }
}
public class B // no table in db
{
public string Property1 { get; set; }
// public C Definition { get; set; }
public int DefinitionId { get; set; }
}
public class C // has table in db
{
public int Id { get; set; }
// public long Id { get; set; }
public string Property2 { get; set; }
} The issue tracking allowing navigations out of a complex type is #31245. We can keep this issue for tracking doing the same, if we believe we'll end up doing the same for owned entity types. |
I want to store
A.Items
as JSON, preserving the propertiesProperty1
ofB
andId
ofC
when serializingItems
. When querying objectA
, I want to be able to use the preservedId
property ofC
in the JSON to retrieve objectC
and assign it to theDefinition
field ofB
.The text was updated successfully, but these errors were encountered: