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

Can't insert into temporary table with Bulk Operations #1196

Open
daaleonardo opened this issue Dec 30, 2024 · 0 comments
Open

Can't insert into temporary table with Bulk Operations #1196

daaleonardo opened this issue Dec 30, 2024 · 0 comments
Labels
question Further information is requested

Comments

@daaleonardo
Copy link

daaleonardo commented Dec 30, 2024

I'm trying to import data into a temporary table using the same connection.

I manually tried adding some values and it works, but when using BulkInsert() gives me an error:

RepoDb.Exceptions.MissingFieldsException: 'There are no database fields found for table '[dbo].[##TempTable]'. Make sure that the target table '[dbo].[##TempTable]' is present in the database and/or at least a single field is available.'

This is my code:

public static void ImportDocuments(List<ArtsoftDoc> docList)
{
        CreateTemporaryTable();

        var toMergeList = docList.Select(doc => new ArtsoftDocDTOTemp(doc)).AsList();

        var temp = con.ExecuteQuery("SELECT * FROM ##TempTable");

        con.ExecuteNonQuery("INSERT INTO ##TempTable (Doc_ID, DocType, DocNumber) VALUES (1, 1, 1)");

        temp = con.ExecuteQuery("SELECT * FROM ##TempTable");

        con.BulkInsert(toMergeList);
}
public static void CreateTemporaryTable()
{
    var createTableQuery = """
        CREATE TABLE ##TempTable (
            Doc_ID INT,
            DocType INT,
            DocNumber BIGINT,
            Partner_ID INT,
            PartnerName NVARCHAR(255),
            ReferenceDoc NVARCHAR(255),
            VendorNumber INT,
            DocDate INT,
            DueDate INT,
            VATAmount DECIMAL,
            NetAmount DECIMAL,
            DocTotal DECIMAL,
            Year INT,
            Balance DECIMAL,
            IsCanceled BIT,
            DocumentNumber NVARCHAR(255)
        );
        """;

    con.ExecuteNonQuery(createTableQuery);
}
[Map("[dbo].[##TempTable]")]
public class ArtsoftDocDTOTemp
{
    public int Doc_ID { get; set; }
    public int DocType { get; set; }
    public long DocNumber { get; set; }
    public int Partner_ID { get; set; }
    public string PartnerName { get; set; }
    public string ReferenceDoc { get; set; }
    public int VendorNumber { get; set; }
    public int DocDate { get; set; }
    public int DueDate { get; set; }
    public decimal VATAmount { get; set; }
    public decimal NetAmount { get; set; }
    public decimal DocTotal { get; set; }
    public int Year { get; set; }
    public decimal Balance { get; set; }
    public bool IsCanceled { get; set; }
    public string DocumentNumber { get; set; }  
}
@daaleonardo daaleonardo added the question Further information is requested label Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant