-
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
InMemory: Cascade Delete Support #3924
Comments
FYI, I am grabbing this one. |
Here is an update of the issue: Why cascade delete isn't working for InMemory database? [Fact]
void test_written_in_notepad()
{
List<int> childIds;
using(var context = CreateInMemoryContext())
{
var parent = context.Parents.Include(p => p.Children).FirstOrDefault(p => p.Id == 1);
childIds = p.Children.Select(c => c.Id).ToList();
context.Remove(parent);
context.SaveChanges();
}
using(var context = CreateInMemoryContext())
{
Assert.Empty(context.Children.Where(c => childIds.Contains(c.Id));
}
} Then why is it working for relational databases?
It turns out, related database are designed to handle that without any tweaking from the EF. All EF has to do is generate the proper SQL (on delete cascade or whatever is appropriate) when creating the tables. What needs to be done for InMemory database to support cascade deletion? What did I do? What's next? [ConditionalFact]
public virtual void Optional_One_to_one_relationships_are_one_to_one()
{
using (var context = CreateContext())
{
var root = context.Roots.Single(IsTheRoot);
root.OptionalSingle = new OptionalSingle1();
Assert.Throws<DbUpdateException>(() => context.SaveChanges());
}
} To support this behavior, it is apparent that implementing cascade delete is not going to be enough. We also need to check Added and Modified entries for relational consistency when saving. I understand that it does not fall under the title of this issue. But, without this implementation, I cannot really make the tests pass- unless I overwrite them to be empty, as they are in the dev branch. So, my question is, should I submit a PR for cascade delete feature only under this issue and implement relational consistency checking in another PR? Or, would you like to change the title/description of the issue so that I can submit a single PR and make all the tests passing? |
@zpbappi The support for cascade delete should be implemented purely in the in-memory database. It should not change the StateManager in any way. It should not change which entities are being loaded. This means that the in-memory database needs to understand the FK constraints that have been created and what their cascade behavior is defined as. It should then follow these constraints, independently of the state manager, when an entity is deleted and make appropriate changes or throw appropriate exceptions based on the constraint and cascade behavior. You would effectively be implementing some part of the FK support that a relational database has. |
This feature would be very welcome. Any updates? |
This issue is in the Backlog milestone. This means that it is not going to happen for the 2.0 release. We will re-assess the backlog following the 2.0 release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. |
Brilliant, thank you :) |
Any updates? I would really appreciate this feature to ensure my test in memory database behaves (more) like my production database. |
This depends on #2166 |
有些测试没有通过,因为InMemoryDB目前还不支持外键相关的Cascade删除(见https://github.com/dotnet/efcore/issues/3924)。考虑在代码里显式删除相关实体,或者使用真实数据库进行测试。
有些测试没有通过,因为InMemoryDB目前还不支持外键相关的Cascade删除(见https://github.com/dotnet/efcore/issues/3924)。考虑在代码里显式删除相关实体,或者使用真实数据库进行测试。
有些测试没有通过,因为InMemoryDB目前还不支持外键相关的Cascade删除(见https://github.com/dotnet/efcore/issues/3924)。考虑在代码里显式删除相关实体,或者使用真实数据库进行测试。
有些测试没有通过,因为InMemoryDB目前还不支持外键相关的Cascade删除(见https://github.com/dotnet/efcore/issues/3924)。考虑在代码里显式删除相关实体,或者使用真实数据库进行测试。
Any updates? |
@komdil This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 7.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you. |
We recommend against using the in-memory provider for testing--see Testing EF Core Applications. While we have no plans to remove the in-memory provider, we will not be adding any new features to this provider because we believe valuable development time is better spent in other areas. When feasible, we plan to still fix regressions in existing behavior. |
Dear all
During my tests with EF RC1, I found, that the EF.InMemory is lacking a quite important feature (IMHO). Cascading deletes are not supported by EF.InMemory!
Having the following context and classes, you can reproduce the issue quite easily:
With the following lines of code, you can see the results:
Output for SQL CE Provider (and all other relational providers)
Output for EF.InMemory
The results for the actual providers is correct (and as expected). The output for EF.InMemory however isn't - or at least to me, it is surprising.
In your documentation I found the following:
Whereas I totally get this for a real context, which would need to evaluate these FK relations in-memory and therefore fetch the entries from the store, I actually cannot understand, why EF.InMemory can't do this for me. It already has the whole graph in-memory and would only need to evaluate the FK relations, which are already in place.
Actually, the EF.Core infrastructure almost does all the things, and handles the delete in the graph correctly. It propagates the DELETE calls along the graph-relations.
Unfortunately, EF.InMemory does not really support this, as shown in these tests.
Why can't EF.InMemory handle this? It would be quite easy to implement I guess.
And why can it delete entries, if they are in the current context? At that point, it already CAN evaluate the FK relations and the cascading deletes. So it could also do for all entries in the store.
For the in-memory case, all entries are always in memory (and thus close to the context). It would be easy to fetch them, in case of a delete call to a cascading FK relation.
Any input to this topic?
Thanks a lot,
Harald
The text was updated successfully, but these errors were encountered: