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

Do not require defining JSON properties with Property #2

Closed
jukkahyv opened this issue Jul 23, 2018 · 1 comment
Closed

Do not require defining JSON properties with Property #2

jukkahyv opened this issue Jul 23, 2018 · 1 comment

Comments

@jukkahyv
Copy link
Contributor

Problem is that without the call to Property(), EF recognizes JSON fields as navigation properties instead of plain properties and tries to look for a primary key.

System.InvalidOperationException: The entity type 'Address' requires a primary key to be defined.

For now, I cannot find a way to remove the navigation property from the entity type.

Related to dotnet/efcore#11199

@WillFvrther
Copy link

WillFvrther commented Aug 24, 2018

Add the following code before the builder.AddJsonFields() call to do the trick.

foreach (var entity in builder.Model.GetEntityTypes())
{
    foreach (var property in entity.ClrType.GetProperties())
    {
        if (property.CustomAttributes.Any(a => a.AttributeType == typeof(JsonFieldAttribute)))
        {
            builder.Entity(entity.ClrType).Property(property.PropertyType, property.Name);
        }
    }
}

This will force all entities clr properties to be seen as an entity property instead of a navigation.
It could be added in the AddJsonFields call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants