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

WHERE clause in generated SQL does not use ColumnAttribute.Name value #6

Closed
wesfincher opened this issue May 20, 2022 · 4 comments · Fixed by #7
Closed

WHERE clause in generated SQL does not use ColumnAttribute.Name value #6

wesfincher opened this issue May 20, 2022 · 4 comments · Fixed by #7

Comments

@wesfincher
Copy link

wesfincher commented May 20, 2022

Given the following entity and column definition:

    [TableName("wcpEvent")]
    [PrimaryKey("EventId")]
    public class Event : EntityBase
    {
        [Column("EventId")]
        public override int Id { get; set; }
        // omitted
    }

and the following base class:

    public abstract class EntityBase
    {
        public virtual int Id { get; set; } 
        // omitted
    }

with the following Repository class

        private readonly IDatabase _context;
        public virtual T? SingleOrDefault(Expression<Func<T, bool>> filter)
        {
            return _context.Fetch(filter).SingleOrDefault();
        }

The following test fails:

        [TestMethod]
        public void Repository_SingleOrDefault_ShouldReturnNull()
        {
            using IUnitOfWork uow =
                new UnitOfWork(ConnectionString);
            var evt = uow.Repository<Event>().SingleOrDefault(e => e.Id == -1); // <-- Id vs EventId
            evt.Should().BeNull();
        }

The following exception is thrown:

Message: 
Test method DataServiceTest.RepositoryTests.Repository_SingleOrDefault_ShouldReturnOne threw exception:
System.Data.SqlClient.SqlException: Invalid column name 'Id'.

The SELECT clause is generated correctly. Expected behavior is for the WHERE clause to generate using the name of the property with ColumnAttribute.Name ('EventId')

@asherber
Copy link
Owner

asherber commented May 20, 2022

@wesfincher What versions are you using of this library and StaTypPocoQueries?

@asherber
Copy link
Owner

Nevermind, I can reproduce. Looks like it has to do with the class being a descendant and the property being an override. I'll have to dig into this; I'm not sure whether the fix will be in this library, or PetaPoco, or StaTypPocoQueries.

@wesfincher
Copy link
Author

wesfincher commented May 21, 2022 via email

@asherber
Copy link
Owner

The SELECT is generated by PetaPoco, using its normal mechanisms. The WHERE is generated when StaTypPocoQueries.Core grabs the MemberInfo for the field you're querying and passes it to this library to figure out the correct column name. The MemberInfo I get shows the DeclaringType as EntityBase, no references to Event, so when I ask PP to help me out with the column name, it's looking at EventBase

So I think the question is whether StaTypPocoQueries is able to pass me a MemberInfo that references the actual runtime class. I'll open a ticket over there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants