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

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

Open
lubofuco opened this issue May 12, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@lubofuco
Copy link

lubofuco commented May 12, 2022

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.

@lubofuco lubofuco added the bug Something isn't working label May 12, 2022
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

1 participant