1

I am trying to create a create method which will insert rows into my database.

Table

This is my database table. As you can see it has an accountId column

CREATE TABLE [dbo].[Transactions](
    [Id] [int] NOT NULL,
    [AccountId] [int] NULL, 
PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

Class

This is my transactions class as you can see the class name is transactions the same as the table and there is an account id column.

public class Transactions
{
    public int Id { get; set; }
    public int AccountId { get; set; }        
}

Create method

Here is my create method which should insert a row into the database.

    public async Task<int> CreateAsync(Transactions transactions)
    {
        using var dbConnection = await _connectionFactory.CreateConnectionAsync();
       return await dbConnection.ExecuteAsync("""
                                                     insert into Transactions (AccountId) 
                                                     VALUES (@AccountId)
                                                     """, new { AccountId = transactions.AccountId});
    }

I have also tried to just pass transactions but it didn't work either so I tried. new { AccountId = transactions.AccountId}

Issue

As you can see from this image no matter what I do it won't detect the columns.

enter image description here

What I have tried

I have chosen the correct schema, I have also tried to disconnect the database and reconnect again.

I am at a loss as to why this doesn't work. I have another method which is get and passes and Id that works, I also have another service which connects to another table and I can create an insert statement for that table.

6
  • What you posted isn't a compilation or even an Intellisense warning. It looks like a Rider feature that tries to validate queries by checking against a configured database. Click on Choose Schema to select the database/schema to use for validation. Don't expect miracles though. If you connect to multiple databases Rider can't know which one should be used each time. Even then, you may get an error because Rider only knows about DbCommand and parameters. It doesn't know how micro-ORMs like Dapper convert properties to parameters Commented Dec 9, 2024 at 8:19
  • 1
    The error you just posted has nothing to do with the warning. Post the full exception and the actual code that throws as text. Either the error has nothing to do with the error, or the code hidden by the message is something like transactions.transactions.AccountId. Commented Dec 9, 2024 at 8:24
  • If you check under what i have tried i have tried to chose schema, and serval other tables are working fine its this one that's not. (I have removed the unrelated error.) Commented Dec 9, 2024 at 8:25
  • What you posted right now isn't an error at all. It's a failed Rider analyzer, nothing more. Rider complains about the @AccountId parameter, because it doesn't know where it came from. Dapper works fine with new { AccountId = transactions.AccountId} or new { transactions.AccountId}. In both cases a new anonymous type with an AccountId property is created, and Dapper maps this to the @AccountId parameter. Commented Dec 9, 2024 at 8:26
  • This is a known unresolved Rider bug Commented Dec 9, 2024 at 8:29

1 Answer 1

2

This is a known unresolved Rider bug - Rider doesn't understand how Dapper maps properties to query parameters and assumes @AccountId is a variable. As a comment under the bug shows, this happens with F# and raw ADO.NET as well.

The code itself should run fine. As long as the names of the anonymous type properties match the parameters, the query will work. This code is no different than the code in Dapper's landing page eg:

var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });

or

var count = connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
    new[] { 
        new { a=1, b=1 }, 
        new { a=2, b=2 }, 
        new { a=3, b=3 } 
    }
  );

You can use :

new { AccountId = transactions.AccountId}

or

new { transactions.AccountId}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.