Skip to content

Program fails at Microsoft.Data.SqlClient.SqlException: 'Must declare the scalar variable "@InvoiceTemplateId". #99

Open
@lubofuco

Description

Library name and version

  • Kros.KORM 4.1.2

Description

Program fails at Microsoft.Data.SqlClient.SqlException: 'Must declare the scalar variable "@InvoiceTemplateId".
Statement(s) could not be prepared.' The exception throws if Select statement contains field with another database column name as defined by model.
Program not throws the exception without Select statement which specified concrete columns.

Steps To Reproduce

string[] columns = new[] { nameof(InvoiceTemplateSettings.DocumentId), nameof(InvoiceTemplateSettings.TemplateName) };
IDbSet<InvoiceTemplateSettings> dbSet = _database.Query<InvoiceTemplateSettings>().Select(columns).AsDbSet();
var documentFlat = new InvoiceTemplateSettings() {
    DocumentId = 7,
    TemplateName = "zmenená šablónka"
};
dbSet.Edit(documentFlat);
await dbSet.CommitChangesAsync();

And the console show

UPDATE [InvoiceTemplateSettings]
SET [TemplateName] = @TemplateName
WHERE ([InvoiceTemplateId] = @InvoiceTemplateId)
WITH PARAMETERS (zmenená šablónka)

Model is

public class InvoiceTemplateSettings
{
    /// <summary>
    /// Invoice template ID.
    /// </summary>
    public long DocumentId { get; set; }
    public string TemplateName { get; set; }
}

Model builder is

modelBuilder.Entity<Entities.InvoiceTemplateSettings>()
    .HasTableName(DatabaseTableNames.InvoiceTemplateSettingsTableName)
    .HasPrimaryKey(f => f.DocumentId)
    .Property(f => f.DocumentId).HasColumnName("InvoiceTemplateId");

SQL

CREATE TABLE [dbo].[InvoiceTemplateSettings] (
    [InvoiceTemplateId] [bigint] NOT NULL,
    [TemplateName] [nvarchar](250) NOT NULL,
    CONSTRAINT [UK_InvoiceTemplateSettings] UNIQUE NONCLUSTERED (
        [InvoiceTemplateId] 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]
)

If I replace the line with the query without .Select(columns) statement:

IDbSet<InvoiceTemplateSettings> dbSet = _database.Query<InvoiceTemplateSettings>().AsDbSet();

the code runs success without exception.

Expected behavior

I think this is a bug because I must use the name of database column, not column of model. I would expect to use model column name.

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions