-
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
Add SQL Server JSON support through backing fields #7394
Comments
Awesome, Thank you for clarifying, I really need this feature. |
@jovanpop-msft In the cases where you are getting the exception EF is still mapping the Tags property because there is no code that tells EF not to map this property. If the expectation is that EF should interact through the _Tags field, then the Tags property will need to be ignored, like you do with NotMapped in the second example. The _Tags field can then be mapped using something like: modelBuilder.Entity<Blog>().Property("_Tags"); |
@ajcvickers Maybe I don't understand you, but according to the https://docs.microsoft.com/en-us/ef/core/modeling/backing-field#controlling-when-the-field-is-used I just need to specify that Property Tags will use field _Tags: modelBuilder.Entity<Blog>()
.Property(b => b.Tags).HasField("_Tags"); This actually works when backing field and property have the same type and this code works fine: internal string _Tags;
public string Tags
{
get { return _Tags; }
set { _Tags = value; }
} Hovewer, this code fails with exception: internal string _Tags;
public string[] Tags
{
get { return (this._Tags == null) ? null : JsonConvert.DeserializeObject<string[]>(this._Tags); }
set { _Tags = JsonConvert.SerializeObject(value); }
} Therefore the only difference is mismatch in the return types string vs string[]. I don't need code like: modelBuilder.Entity<Blog>().Property("_Tags"); since backing fields that starts with _ are automatically used as regular properties (or I'm missing something in this case). Case with identical types works fine. I can explicitly ignore Tags property, but this is not needed when I have the same types. modelBuilder.Entity<Blog>().Ignore(b => b.Tags); |
@jovanpop-msft It has different semantics in the model. In the case where the property is mapped, then the metadata for the property in the model comes from the type of the property. So even if you say that EF should read and write that property using the field, it doesn't change that EF reasons about it using the type of the property. This impacts how APIs such as EF.Property or property entries work. In this case, EF doesn't know how to deal with string[] and so there will always be some type of exception thrown. This is different from EF reasoning about a conceptual property of type string but having that property not represented by any actual CLR property, which is the case in your class. In that case you have to set up a property of type string and tell EF to map it to a field. The shorthand way of doing this is the code I posted. |
Closing as we already have the following issues tracking the high level scenario here:
As @ajcvickers outlined, backing fields need to match the data type of the property they are backing. |
@jovanpop-msft |
I would like to define a property of string[] type that would be serialized as JSON text in database (I don't want to have a separate table for list of tags).
I'm trying to use backing field where I would serialize/deserialize property using JSONConvert class:
However, OnModelCreating throws the following error:
There should be no limitation in this case since I'm defining my serialization logic in getter/setter, and EF should just persist string in Tags field into Tags column in a table.
Since we can do the similar thing using included/excluded fields, it is strange that the same functionality is not supported in backing fields:
Note
This feature might be used to implement JSON support in EF if we have the following:
where Person is just a plain class, e.g.:
Steps to reproduce
Further technical details
EF Core version: 1.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2015
The text was updated successfully, but these errors were encountered: