Dapper.Contrib | Dapper community contributions - additional extensions
kandi X-RAY | Dapper.Contrib Summary
kandi X-RAY | Dapper.Contrib Summary
Dapper community contributions - additional extensions for Dapper
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Dapper.Contrib
Dapper.Contrib Key Features
Dapper.Contrib Examples and Code Snippets
Community Discussions
Trending Discussions on Dapper.Contrib
QUESTION
I have code like this:
...ANSWER
Answered 2021-Aug-06 at 09:58The Insert
method of Dapper Contrib does not return the newly generated ID. This is because Contrib is built over Dapper which neither map the newly generated ID to the entity by default nor does it returns it. But there are ways to return the newly generated ID using Dapper. Those are discussed here, here and here.
Dapper Extensions (other query generator for Dapper) support this by default. Please refer to this answer for more details.
One way is to bypass the Contrib and use Dapper; use any of the way explained in links to get the newly generated ID (OUTPUT
parameter or @@IDENTITY
etc.).
Other way to do this with Contrib is to get the newly generated ID by separately calling Dapper (as mentioned in links; @@IDENTITY
) immediately after a call to Insert
.
Third way is to explicitly assign the ID instead of automatically generating it. You can use [ExplicitKey]
attribute to achieve this. You also need to change your database accordingly.
[Key]
should be used for database-generated keys (e.g. autoincrement columns), while[ExplicitKey]
should be used for explicit keys generated in code.
More aggressive solution is to modify the Contrib code to make Insert
return newly generated ID.
By the way, I found [https://github.com/DapperLib/Dapper.Contrib/issues/58] issue similar to your problem; see if that helps you.
QUESTION
I'm trying to get all records from SQL database using DapperExtensions.
But I have a Schema set to other than dbo
for some tables. Hence, the table is not recognized from sql query.
For example, a table is in the form [Schema][TableName]
. But when I start query, error is thrown like:
Invalid object name 'TableName'.
This is the Model class:
...ANSWER
Answered 2021-Jul-13 at 13:59While mapping your models, be bit more explicit. Mention the Schema
explicitly.
Following is an example how to provide various mapping properties.
QUESTION
My application is .net 5
with Dapper.Contrib
.
My entity name is Tax
and database table name is pluralized Taxes
.
When I run any CRUD method of the Dapper
library I get an exception:
Invalid table name Taxs.
Dapper adds "s" at the end, but what if I need "es"?
...ANSWER
Answered 2021-Jun-28 at 21:31You can use the TableAttribute
of the Dapper.Contrib.Extensions
namespace.
QUESTION
I am using jquery datatables to display a table of data. The table is only displaying 3 columns from my SqlFunction.
I use dapper like this:
...ANSWER
Answered 2021-Jun-07 at 14:37This is not caused by Dapper, it is caused by JsonSerializer
accessing the property.
If you add the [JsonIgnore]
attribute to your property then it will not be accessed by JsonSerializer
QUESTION
I used Dapper.Contrib in my Asp.Net Core Web API project. I encountered a problem while writing a test with xUnit in this project. For example, here is my method that adds records to my data layer.
...ANSWER
Answered 2020-Aug-24 at 06:05I believe what is happening is you are using a mocked connection and trying to call a Dapper method, InsertAsync
, on it. The Dapper method is inevitably failing because it is not a real connection.
I'm not sure how much value you get using mocks here. What you really want to know, when testing AddAsync, is does it actually do what you want it to do which is insert data into the database. So if I were you I would turn this test into an integration test by using a real connection instead of a mocked one. One way of doing this is to
- Use a test database for the purposes of testing
- Before running the test delete all data from the test database
- In your assert use Dapper or otherwise to check that a query for the entity brings back the data you expect to be in the database.
I don't necessarily recommend this but another approach could be to use an in memory database. See for example unit testing Dapper Repositories.
QUESTION
I'm setting up a Mock for various test cases:
...ANSWER
Answered 2020-Jul-20 at 18:52Turns out it was a type mismatch that slipped my eyes. Since the signature of IDbConnectionExtensionsWrapper.Get(...)
takes an int?
and I was setting up the mock with It.IsAny()
, there was no suitable call to the method (it was never called).
The correct way to mock the method would be
QUESTION
I have a .Net Core 3.1 Console application.
In SQL Server database I have tables with singular names, the same as my POCO classes, which is convenient for matching and maintaining.
For Insert, Update and Delete operations I want to use Dapper.Contrib
library. But when I run Insert function Dapper pluralize table names in generated SQL queries.
SQL table "Student":
...ANSWER
Answered 2020-Mar-09 at 13:14Your implementation has a couple of problems but, because you're essentially returning type.Name
, it should be returning the name of your type (in this case Student
).
First let's address the problems with your current implementation of TableNameMapper
;
You only need to set the delegate once. You can do this somewhere like the
ConfigureServices
of yourStartup
class. Right now you're setting it everytime you open your connection.Your implementation executes
type.Name.Remove(type.Name.Length - 1, 1);
but you're not assigning the result of the operation to a variable. Even if you had assigned the result you're just returningtype.Name
anyway.
I ran the code you have above and it works correctly for me in LINQPad. I'd suggest adding a breakpoint in your debugger on the return type.Name;
line of your delegate. If you need a hand debugging then take a look at the Visual Studio debugging documentation. Check what type.Name
actually is and go from there.
Here's the code I ran verbatim:
QUESTION
I upgraded my .net core 2.2 web api to .net core 3.1. I am now able to run it from console using dotnet myapi.dll
, however when I try to host it on ISS, I get following exception.
I have installed AspNetCore Module V2
and this module is throwing exception and I am not able to understand the missing part.
Autofac.Core.DependencyResolutionException: An exception was thrown while activating ?:Microsoft.Extensions.Hosting.IHostedService[] -> Microsoft.AspNetCore.Hosting.GenericWebHostService -> Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer.
---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.AspNetCore.Server.IIS.Core.IISNativeApplication, Microsoft.Extensions.Hosting.IHostApplicationLifetime, Microsoft.AspNetCore.Authentication.IAuthenticationSchemeProvider, Microsoft.Extensions.Options.IOptions``1[Microsoft.AspNetCore.Builder.IISServerOptions], Microsoft.Extensions.Logging.ILogger
Following is my Program.cs
ANSWER
Answered 2020-Feb-10 at 16:07Remove the Microsoft.AspNetCore.* nuget packages, those no longer exist in 3.0 so you're pulling in 2.x versions. https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#remove-obsolete-package-references
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Dapper.Contrib
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page