-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
EF Core 8 + MVVM - ObservableCollection<string> gives errors in TPC #30459
Comments
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
I've added the repro steps in the comments, thank you. |
@DJRM2021 Collections of primitive types are not yet supported by convention--see #25163 and #29427. You can use a value converter for this, as documented here: For example: https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions?tabs=data-annotations#examples modelBuilder.Entity<Teacher>().Property(e => e.Competencies).HasConversion(
v => JsonSerializer.Serialize(v, (JsonSerializerOptions)null),
v => JsonSerializer.Deserialize<ObservableCollection<string>>(v, (JsonSerializerOptions)null),
new ValueComparer<ObservableCollection<string>>(
(c1, c2) => c1.SequenceEqual(c2),
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
c => new ObservableCollection<string>(c))); |
@ajcvickers Thanks for the prompt reply and assistance. I'm hoping support for collections of objects will be added in EF Core 8. |
I am using EF Core 8 Preview 1.2 and CommunityToolkit.Mvvm 8.1.0 in a WASDK 1.2/WinUI 3 C# desktop app project.
Here are my models:
Here is my DbContext:
Here is the error when running Add-Migration in the PM console:
PM> Add-Migration D Build started... Build succeeded.
Unable to create a 'DbContext' of type ''. The exception 'The entity type 'ObservableCollection' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Error is not supposed to occur because Teacher inherits from Employee which inherits from Person. Person base class has the [Key] annotation which tells EF core which primary key to use. Am I missing something here? The project compiles without any errors. It's the migration that fails.
I am using Visual Studio Community Edition 17.5.1
The following NuGet packages are installed:
Microsoft.EntityFrameworkCore.SqlServer 8.0.0-preview.1.23111.4
Microsoft.EntityFrameworkCore.Tools 8.0.0-preview.1.23111.4
Microsoft.EntityFrameworkCore.Design 8.0.0-preview.1.23111.4
CommunityToolkit.Mvvm 8.1.0
The text was updated successfully, but these errors were encountered: