Open kabua opened 9 years ago
Hmmm... so should the default EF repository logic change to just call new?
I just go the same issue with trying to create a Group, and I'm currently block on this issue.
I think you might be right, that perhaps the EF logic should be changed to calling 'new'.Otherwise, I don't think any of the RegisterXXXXXForDelete will work as expected.
Or perhaps just change:
public override TGroup Create()
{
return items.Create();
}
to
public override TGroup Create()
{
return items.AsNoTracking().Create();
}
So that the new item isn't being tracked, I'm testing it now. I'll let you know what I find.
That didn't work because I was working on a cached file and didn't realized that I couldn't call Create()
after .AsNoTracking()
. So, I tried this:
var item = items.Create();
db.Entry(item).State = EntityState.Detached;
return item;
but that didn't work. However, this did:
public override TGroup Create()
{
return new TGroup();
}
Had to change
where TGroup : RelationalGroup
to
where TGroup : RelationalGroup, new()
Hope that helps.
The Bug
I will explain the bug and then a simple work-a-round.
The old Issue is a valid bug. But the bug isn't coming from the fact that @corpsekicker was creating a custom User Account. The bug is coming from the fact that DbContextUserAccountRepository's
is called.
This call is triggered when CreateAccount is called where the
account
parameter is set to null. Thus, when the following code is calledthe CreateUserAccount() is executed. Which eventually calls into DbContextUserAccountRepository's
Note that
items
is defined as aDbSet<TAccount>
, therefore, this call creates a User Account proxy which is already associated with EF.Then when
is called (a few lines after the
account = account ?? CreateUserAccount();
line), theDbModelBuilderExtensions.RegisterUserAccountChildTablesForDelete(...)
is triggered. Which runs this set of codeBecause the
account
is 'known' by EF (and is marked at 'added') then none of the account's collections can be de-referenced. If they are de-referenced, as inaccount.ClaimCollection
, then EF throws the following exceptionThe good news - this bug is easy to work-a-round.
The Work-a-Round
Call
CreateAccount
with an existingaccount
. For exampleSo, when the
account.ClaimCollection
is de-reference the value isnull
and the extension method works without complaining. Thus everything works just fine.Happy Coding!