Repository navigation
Description
Users of my EfCore.SchemaCompare library want it to work on databases that uses entries that use JSON column mappings.
My EfCore.SchemaCompare library allows a developer to compare the DbContext.Model against an existing database and it tells you if there are differences between the two sources. The What does EfSchemaCompare check? section in to README tells what the library checks.
To do this I had to dig into all sorts EFCore's metadata and what they do (see EfSchemaCompare/Internal/Stage1Compare to get a feel what I did to create this library.)
I'm currently looking for metadata that tells me what properties are held in a JSON string, but I can't anything so far. I should say that I'm not as good developer as I used to be because I have dementia now (see this section in one of my articles).
What I have found so far
A JSON column is an Owned Type, which makes sense. However its not obvious how you know that it uses JSON columns. I'll keep looking.
Here is my test code
I don't think you need this, but these are the classes and DbContext I'm using at the moment.
classes
public class HeadEntry
{
public int Id { get; set; }
public int HeadInt { get; set; }
public OuterJsonMap JsonParts { get; set; }
/// <summary>
/// Useful for debugging
/// </summary>
/// <returns></returns>
public override string ToString()
{
return $"{nameof(HeadInt)}: {HeadInt}, {nameof(JsonParts)}: {JsonParts}";
}
}
public class OuterJsonMap
{
public int OuterInt { get; set; }
public string OuterString { get; set; }
public DateTime OuterDate { get; set; }
public InnerJsonMap InnerJsonMap { get; set; } = null!;
/// <summary>
/// Useful for debugging
/// </summary>
/// <returns></returns>
public override string ToString()
{
return $"{OuterInt}, {OuterString}, {OuterDate}, {InnerJsonMap}";
}
}
public class InnerJsonMap
{
public int InnerInt { get; set; }
public string InnerString { get; set; }
public DateTime InnerDate { get; set; }
/// <summary>
/// Useful for debugging
/// </summary>
/// <returns></returns>
public override string ToString()
{
return $"{InnerInt}, {InnerString}, {InnerDate}";
}
}
public class Normal
{
public int Id { get; set; }
public string NormalString { get; set; }
public int NormalExtraId { get; set; }
public NormalExtra NormalExtra { get; set; }
}
public class NormalExtra
{
public int NormalExtraId { get; set; }
public string ExtraString { get; set; }
}Test DbContext
public class JsonCustomerContext : DbContext
{
public JsonCustomerContext(DbContextOptions<JsonCustomerContext> options)
: base(options) {}
public DbSet<Customer> Customers { get; set; }
public DbSet<Normal> Normals { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().OwnsOne(
headEntry => headEntry.JsonParts, ownedNavigationBuilder =>
{
ownedNavigationBuilder.ToJson();
ownedNavigationBuilder.OwnsOne(contactDetails => contactDetails.Address);
});
}
}Include provider and version information
EF Core version: 8.0.4
Database provider: all
Target framework: .NET 8.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.11
Activity
[-]Help to detect that a propery is stored in a JSON string (not urgent)[/-][+]Help to detect that a propery is stored in a JSON string[/+]cincuranet commentedon Sep 5, 2024
You can use
IsMappedToJsonmethod for this. Here's an example based on your code:This will print
TruebecauseJsonPartsis mapped usingToJson. For other properties it will returnFalse.JonPSmith commentedon Sep 6, 2024
HI @cincuranet,
Thank you very much - that put me on the right track, and I'm getting there.
I have some other questions if you don't mind:
OnModelCreatingsetup? I tried to do that in theOnModelCreatingmethod, but couldn't find a way to do that.cincuranet commentedon Sep 6, 2024
What database string?
You mean Json1 and Json2 as properties in entity? Then the answer is yes.
JonPSmith commentedon Sep 6, 2024
Wow, quick. I edited my question, which I hope will be clear.
cincuranet commentedon Sep 6, 2024
Yes. The
ToJsonmethod has overload that takes string.JonPSmith commentedon Sep 9, 2024
Hi @cincuranet,
Thanks for your help on this and I have got JsonMapping working. I'm closing this now, but I have some more things to do before I release EfCore.SchemaCompare 8.1.0.
JonPSmith commentedon Sep 28, 2024
Hi,
I have reopen this issue as I have another issue about detecting if a an entity is using Json Mapping. In my first attempt I had a series of entries in hierarchically using Json Mapping. e.g.
In this case I know that the
BottomJson Mapping entry must be a Json Mapping.BUT I added a second Json Mapping entity to the
Headto check it would work, e.g.Then I get two Navigation, but the
ExtraJsondid have anything to tell me that this entity is using Json MappingCan you give me some help me to detect that the
ExtraJsonentity is using Json Mapping.roji commentedon Sep 29, 2024
/cc @cincuranet @AndriySvyryd
AndriySvyryd commentedon Oct 3, 2024
@JonPSmith You need to call
ToJson()onExtraJsonPartsfor it to be mapped to JSON. This would map it to a separate JSON column:JonPSmith commentedon Oct 7, 2024
Hi @AndriySvyryd
What I want a simple
DbContextto use in my testing my code to compare Json Mapping entities against a database. For my tests I have one entity that has two ToJson-type properties, e.g.And after trying various
OnModelCreatingJson Mappings code I found one that creates a database with the correct columns.BUT, when I save data with a
SaveChangesit has aSystem.ArgumentNullException Value cannot be null. (Parameter 'key'). I have looked at this a lot, but can't find the problem is. Below is the the full logging for the test, which occurs whenSaveChangesis called.Here is my entities
Using EF Core 8
AndriySvyryd commentedon Oct 7, 2024
It's caused by the extra
.ToJson()calls on the nested entity types.Change your code to:
We'll detect this case in validation in future releases: #34816
JonPSmith commentedon Oct 8, 2024
Hi @AndriySvyryd,
Thank you, that fixed it.
I would say that there aren't much documentation about the configuration of the Json Mapping in the
OnModelCreatingpart of the the DbContext, and if you get it wrong (like I did) its hard to work out what is wrong. I suggest you add a section called "Json Mapping" in the EF Core's "Create a model" section.roji commentedon Oct 8, 2024
@JonPSmith that's a good suggestion - we're indeed lacking good docs on this. For EF 10 we'll hopefully also allow mapping JSON columns as complex types (as opposed to owned entities), at which point it would become the default/recommended way to do things. We should definitely provide good documentation on this.