-
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
Support fixed-size collections #24497
Comments
Note for triage:
|
Putting this on the backlog to check the first time we create a property accessor. This won't help in all cases because the property can be assigned a new instance of a different type at any time, and we don't want to do an additional check every time we access the property. |
Hi, I have exactly the same problem on the latest version. Include provider and version information EF Core version: 5.0.10 Here is the log: |
@FrankIceBass Fixed-size collections, such as arrays, are not supported since EF needs to add elements to them. |
@ajcvickers I hardly understand which is the fixed-size collections. I'm adding items to a DbSet with AddRangeAsync.
|
@FrankIceBass Typically a collection of one entity type on another entity type. For example, the collection of posts on a blog. This is known as a navigation. See Relationships in the docs. |
@ajcvickers I really hardly understand. I'm using scaffolded db context and it is just a 1-N table. I will try to check more why this "feature" is annoying me so much. Anyway.. thank you for your support. |
@FrankIceBass Feel free to open a new issue attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
@FrankIceBass I'd guess that you are using sth like Array to initialize the navigation property from 1 to N for your relation somewhere during the entity instance initialization. See the repro I attached initially, it might shed some light. |
@v-zubritsky unfortunately it's not my case. I have the same exception even trying to add items not related to parent entities. var list = projectList.Where(item => !userProjects.Where(y => y.ProjectId == item.Id).Any())
|
Hi everyone, I just solved it... well... it's strange solution but it worked for me. I was watching |
Calling |
I feel the need to weigh in here, having encountered this error and scratched my head for over an hour as to where the fixed array was, why it updated and why it was failing to 'Remove' with an 'Add'. Firstly, the exception misled me because I was performing a Secondly, the fixed array was not in either [Table]
public class A
{
[Key]
public int AId { get; set; }
public IList<B>? Bs { get; set; } = new List<B>();
}
[Table]
public class B
{
public int AId { get; set; }
public int CId { get; set; }
[ForeignKey("AId")]
public IList<A> A { get; set; }
[ForeignKey("CId")]
public IList<C> C { get; set; }
}
[Table]
public class C
{
[Key]
public int CId { get; set; }
//I'd set as an array to avoid updating from here.
public IEnumerable<B> Bs { get; set; } = Array.Empty<B>();
} Then when trying to add an In brief: MyDbContext db = new(options);
//Perhaps I loaded some Cs earlier on in the same DbContext, bringing them into the local change tracking, perhaps why clearing the change tracking has worked for others
var bunchOfCs = db.Cs.ToList();
A myA = new();
myA.Bs.Add(new B() { AId = 123, BId = 234 });
db.As.Add(myA); //throws error Changing the In conclusion, it might be a good idea to add a warning of this type of background behaviour along with the exception. |
How is it not possible to take a fixed sized array and convert it into an array that is growable and not fixed? |
Arrays in C#/.NET are always fixed size. |
Description
Error message is not that descriptive as it could be for a case when fixed size collection (Array) is used for a navigation property.
It'd be nice to be pointed to the problematic navigation property or entity type at least with a clean problem description in the error message.
Creating issue because of the conversation happening here #6589 (comment)
Code
I reproduced the issue on a sample used for another issue in this repo previously, so it's not that minimal (and won't probably work even, when this fixed collection case is improved, due to another configuration error), but should be okay for the error discussed.
Repro.zip
Include provider and version information
EF Core version: 3.1.12
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: net48
Operating system: Windows 10
IDE: Visual Studio 2019 16.3
The text was updated successfully, but these errors were encountered: