Skip to content
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

Bug: the DeleteAll<T>(T entities) method results in System.InvalidOperationException errors #1011

Closed
cajuncoding opened this issue Dec 14, 2021 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@cajuncoding
Copy link
Collaborator

cajuncoding commented Dec 14, 2021

Bug Description

While trying to pass in a set of models/entities to be deleted using the DeleteAll<T>(T entities, ...) method we recieve a System.InvalidOperationException exception is thrown.

This is reproducible in our integration test whereby we create two new records using mock data and use RepoDb to Insert hte data, and populate the Identity ID values immediately. This works perfectly as expected.

Then after validating in the test, we attempt to clean up the data by deleting those very same models using the DeleteAll() method passing in the entities themselves. The expected behavior is that RepoDb would recognize the models, correctly resolve the primary Key, and then use that to explicitly delete those records only from the database without any exceptions being thrown.

Our models have Identity ID values that are part of a base model class of which all Models inherit from. When using a flat model it works, but when using a Model that inherits from our base class it fails:
image

Exception Message:

System.InvalidOperationException
  HResult=0x80131509
  Message=The declaring type of the property is not equal to the target entity type.
  Source=RepoDb
  StackTrace:
   at RepoDb.ClassExpression.GetPropertyValuesCache`2.Guard(ClassProperty property)
   at RepoDb.ClassExpression.GetPropertyValuesCache`2.<Do>d__3.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at RepoDb.Extensions.EnumerableExtension.AsList[T](IEnumerable`1 value)
   at RepoDb.DbConnectionExtension.DeleteAll[TEntity](IDbConnection connection, IEnumerable`1 entities, String hints, Nullable`1 commandTimeout, IDbTransaction transaction, ITrace trace, IStatementBuilder statementBuilder)
   at RepoDb.Fix.StarterKit.Repositories.TesterRepository.DeleteAll(IEnumerable`1 entities) in D:\Develop\GitHub.OpenSource\RepoDB\Issue991-RefactoredToFailAsExpected\RepoDb.Fix.StarterKit\Repositories\TesterRepository.cs:line 116
   at Program.<Main>$(String[] args) in D:\Develop\GitHub.OpenSource\RepoDB\Issue991-RefactoredToFailAsExpected\RepoDb.Fix.StarterKit\Program.cs:line 49

Schema and Model:

Please share to us the schema of the table (not actual) that could help us replicate the issue if necessary.

CREATE TABLE [dbo].[EnumTable](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[StringValue] [nvarchar](64) NULL,
	[IntValue] [int] NULL
) ON [PRIMARY]
GO

And also the model that corresponds the schema.

namespace RepoDb.Fix.StarterKit.Models
{
    public class EntityModel : BaseEntityIdModel
    {
        public string StringValue { get; set; }
        public LinkType IntValue { get; set; }
    }

    public class BaseEntityIdModel
    {
        //BBernard: In our Model design we have a base class with common shared properties (actually multiple levels deep) but otherwise these are merely DTO Entity Models!
        [Identity]
        public int Id { get; set; }
    }
}

Library Version:

Example: RepoDb v1.12.10-beta4 and RepoDb.SqlServer v1.1.5-beta4

@cajuncoding cajuncoding added the bug Something isn't working label Dec 14, 2021
@cajuncoding
Copy link
Collaborator Author

Here is a functioning RepoDb Startup app that reproduces the issue 😊
DeleteAll Issue#1011- Startup Failing Repro.zip

@mikependon mikependon pinned this issue Dec 14, 2021
mikependon added a commit that referenced this issue Dec 14, 2021
mikependon added a commit that referenced this issue Dec 14, 2021
@mikependon
Copy link
Owner

The fix will be available on the next version of the core library (>= RepoDb v1.12.10-beta5)

@cajuncoding
Copy link
Collaborator Author

Excellent news! 👏👏👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants