C
C#3y ago
Foxtrek_64

LINQPad 7 - Insert Record [Answered]

Hi all, I'm using LINQPad to insert records from json into a SQL database. All of the documentation seems to suggest that it builds a model based on the table that is a singular name of the table (e.g., if the table name is Regions, the model is named Region). However, this does not seem to be the case for my table, and unfortunately LINQPad seems to lack any sort of intellisense for inspecting the namespace for the correct type name. My table is RVIDocumentCodes but there does not appear to be an RVIDocumentCode type. The InsertOnSubmit() line has an expected error because jsonDocument is the json model. This error is "Cannot convert from UserQuery.RVIDocumentCodeJson to LINQPad.Users.RVIDocumentCodes". This tells me that the RVIDocumentCodes type is my model, but there does not appear to be a constructor that I'm allowed to consume. Both of these lines tell me that RVIDocumentCodes doesn't contain a constructor that accepts that many arguments.
foreach (var jsonDocument in documents)
{
// var document = new RVIDocumentCodes(jsonDocument.DocumentCode, jsonDocument.DocumentTitle, jsonDocument.DocumentUrl);
var document = new RVIDocumentCodes(0, jsonDocument.DocumentCode, jsonDocument.DocumentTitle, jsonDocument.DocumentUrl);
RVIDocumentCodes.InsertOnSubmit(jsonDocument);
}
SubmitChanges();
foreach (var jsonDocument in documents)
{
// var document = new RVIDocumentCodes(jsonDocument.DocumentCode, jsonDocument.DocumentTitle, jsonDocument.DocumentUrl);
var document = new RVIDocumentCodes(0, jsonDocument.DocumentCode, jsonDocument.DocumentTitle, jsonDocument.DocumentUrl);
RVIDocumentCodes.InsertOnSubmit(jsonDocument);
}
SubmitChanges();
What am I missing here?
3 Replies
Foxtrek_64
Foxtrek_64OP3y ago
I did find this gist that supposedly contains a guide for inserting, among other things, records into a table with LINQpad, which seems to mirror the ADO.NET documentation https://gist.github.com/troygoode/3998228 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/how-to-insert-rows-into-the-database
Gist
Add/Update/Delete With LINQPad
Add/Update/Delete With LINQPad. GitHub Gist: instantly share code, notes, and snippets.
How to: Insert Rows Into the Database - ADO.NET
Learn to insert rows into a database by adding LINQ to SQL objects to a table-related collection. LINQ to SQL translates additions to SQL INSERT commands.
Foxtrek_64
Foxtrek_64OP3y ago
Seems I found the answer:
foreach (var jsonDocument in documents)
{
var document = new RVIDocumentCodes()
{
DocumentCode = jsonDocument.DocumentCode,
DocumentTitle = jsonDocument.DocumentTitle,
DocumentUrl = jsonDocument.DocumentUrl
};
RVIDocumentCodes.InsertOnSubmit(document);
}
SubmitChanges();
foreach (var jsonDocument in documents)
{
var document = new RVIDocumentCodes()
{
DocumentCode = jsonDocument.DocumentCode,
DocumentTitle = jsonDocument.DocumentTitle,
DocumentUrl = jsonDocument.DocumentUrl
};
RVIDocumentCodes.InsertOnSubmit(document);
}
SubmitChanges();
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server