-
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
CSharpHelper: support more types of collection literals #19274
Comments
@AdrianoAE-MA It would probably help here if you could describe your goal in some more details. What is the reason for the annotation and how do you intend to use it? We will consider updating the C# helper to support generating literals from IEnumerables. |
I'm trying to learn some of the inner working of the entity framework so I can build some extensions. In this particular case I ended up just refactoring the code to use a static configuration class. During the process I just ended up in that class and while trying to understand it I got to that solution that (for the knowledge I have) makes no sense how the value goes to the actual annotation while I'm just passing to the CSharpHelper a array of empty strings. In terms of what I'm trying to do, I'm trying to automate localization for my domain but I don't want my domain to have any knowledge of it since it's not it's responsability. Giving a concrete example:
In my persistence I've created some EntityTypeBuilder extension where I can configure like that
And since I don't have access to the LanguageEntity (because it's a completely different project) I had to allow some way to configure a "ShadowTable" where I had to say what are the primary Keys (that's where that problem emerged. I was trying to use a Annotation in the model builder to get those values into my extension.
Repository of the actual project: https://github.com/AdrianoAE/AdrianoAE.EntityFrameworkCore.Translations Now I'm stuck trying to understand how I can allow my entity to be used normally and on save changes / on querying use the selected language key and get my domain object. No luck so far 😕 |
@AdrianoAE-MA That's a fairly ambiguous goal. I'm not sure this will be possible without significant hacking. With regards to the original question, we are putting this on the backlog to support more collection types. For now you should be able to just use an array instead of a List. |
@ajcvickers Regarding the CSharpHelper what I don't understand is why a empty array of string works.
I was passing to the Annotation
And then during the OnModelCreating I was casting that annotation back to IEnumerable. That's when I got the error in the CSharpHelper but this works. In terms of the actual goal, I know that it will require quite a lot of work and probably won't be able to do the way I was originally thinking. But at least It's being a good project to help learn some of the internals of EFCore even tho in a very basic level. |
Because the code only handles arrays, not Lists.
It's not casting to |
Fixes dotnet#19274 Also relates to npgsql/efcore.pg#2402
I'm going to implement List literals in CSharpHelper and support for ListInit Expressions which should make this work as well as add support needed by: |
Fixes dotnet#19274 Also relates to npgsql/efcore.pg#2402
Fixes dotnet#19274 Also relates to npgsql/efcore.pg#2402
Fixes dotnet#19274 Also relates to npgsql/efcore.pg#2402
Fixes dotnet#19274 Also relates to npgsql/efcore.pg#2402
* Add support for CSharpHelper for List literals Fixes #19274 Also relates to npgsql/efcore.pg#2402 * Fixes from review comments
Leaving open to support yet other collection literal types; Dictionary specifically could be useful for JSON scenarios. |
If I was to suddenly implement Dictionary literals, would you prefer the pre .NET 6 syntax using collection initializers i.e.: new Dictionary<string, string>
{
{ "key1", "value1" },
...
} or the .NET 6 indexed based variety: new Dictionary<string, string>
{
["key1"] = "value1",
...
} I know this would go into EF for .NET 7 but I didn't know if we prefered to be backward compatible. Also should we put a space after the |
I've been hacking around trying to extend the modelbuilder and I came across this CSharpHelper and need some clarifications.
Class:
And i'm adding a annotation to the EntityTypeBuilder
But then when creating the migration the following error is thrown
So after digging around I got to the CSharpHelper and the UnknownLiteral. After messing around trying to understand what I had to do I ended up doing this for no reason and it worked
After that and injecting my custom CSharpHelper everything works and I can use my List as value of the annotation. But how does it work? Why passing a empty array of strings with the size of my List works and the actual value can be casted back to the List when building the model. What is going on under the hood?
I couldn't find mutch information about the CSharpHelper or even how to implement the UnkownLiteral, I just got it working by accident and I don't understand why it works.
The text was updated successfully, but these errors were encountered: