dapper

How do I handle Database Connections with Dapper in .NET?

江枫思渺然 提交于 2019-11-27 19:13:22
问题 I've been playing with Dapper, but I'm not sure of the best way to handle the database connection. Most examples show the connection object being created in the example class, or even in each method. But it feels wrong to me to reference a connection string in every clss, even if it's pulling from the web.config. My experience has been with using a DbDataContext or DbContext with Linq to SQL or Entity Framework, so this is new to me. How do I structure my web apps when using Dapper as my Data

Using Dapper to map more than 5 types

微笑、不失礼 提交于 2019-11-27 18:51:46
I am currently building a SELECT query that joins 12 tables together. I've been using Dapper for all my other queries and it works great. Problem is, the generic methods only have to five generic parameters. I've previously modified the code to support up to 6 for another query, but now I really don't think I should be hacking 6 more levels of generics. Is there a way to pass dapper an array of types, and it returns the results as an array of objects, which I can cast manually if I have to? I also might be approaching the problem the wrong way! Any help will be appreciated! Radek In a project

Alternative way to get output parameter from stored procedure

此生再无相见时 提交于 2019-11-27 18:24:21
问题 I love using Dapper for my ORM needs but I know there must be a better way to insert/update my sql server database using a stored procedure and strongly typed Lists. For example: I have a class Song: public class Song { public int Id { get; set; } public string title { get; set; } public string genre { get; set; } } and somebody submits a List of songs: List<Song> songs = new List<Song> { new Song { Id = 1, title = "Song 1" , genre="rock"}, new Song { Id = 2, title = "Song 2" , genre="disco"}

Dapper & MS Access - Read works, Write doesn't

南笙酒味 提交于 2019-11-27 18:09:09
问题 Let's start by getting this out of the way: I'm stuck using an MS Access DB and I can't change it. This works fine: using (OleDbConnection conn = ConnectionHelper.GetConnection()) { conn.Open(); var results = conn.Query<string>( "select FirstName from Students where LastName = @lastName", new { lastName= "Smith" } ); conn.Close(); } This works fine: using (OleDbConnection conn = ConnectionHelper.GetConnection()) { OleDbCommand cmd = new OleDbCommand( "update Students set FirstName =

How do I select an aggregate object efficiently using Dapper?

岁酱吖の 提交于 2019-11-27 17:38:31
Lets say that I have a series of objects that form an aggregate. public class C{ public string Details {get;set;} } public class B{ public string Details {get;set;} public List<C> Items {get;set;} } public class A{ public long ID {get;set;} public string Details {get;set;} public List<B> Items {get;set;} } using Dapper, what is the best way to populate these from tables in a database (in my case it's postgres but that shouldn't matter). The tables in the example are pretty much one for one with the object model. The Items property on the class representing foreign key relationships to each

Dapper TypeHandler.SetValue() not being called

*爱你&永不变心* 提交于 2019-11-27 16:32:27
问题 I am testing Dapper to load / persist objects to an Oracle database, and to manage Oracle's Guid storage I need a SqlMapper.TypeHandler<Guid> . When loading a Guid column from the database the Parse method is called, but when I attempt to execute an SQL statement using a Guid parameter I get the following exception: System.ArgumentException was unhandled; Message=Value does not fall within the expected range.Source=Oracle.DataAccess. In debug I can see that my handler's Parse() method is

Map string to guid with Dapper

巧了我就是萌 提交于 2019-11-27 14:57:56
问题 I'm using Dapper to hammer out some load testing tools that need to access a PostgreSQL database. This particular version of PostgreSQL does not support GUIDs natively, so GUID values are stored as 32 character strings. The values are converted to strings using someGuid.ToString("N") , conversion back to Guid can be done using new Guid(stringValueFromColumn) . My question is how do I get Dapper to read the strings and convert them back to Guids? I tried modifying the DbType mapping but that

How to log/get a SQL query auto-generated by Dapper Extensions?

倖福魔咒の 提交于 2019-11-27 14:50:47
I am using Dapper Extensions (DE) as ORM. It is consumed in Data Access Layer which is implemented using Repository pattern. SQL Express is back-end RDBMS. DE automatically generates most of the queries for me. I want to log those auto-generated queries for debugging purpose. There are two ways I can see to achieve this: - Get the SQL query generated by DE (before or after it is executed) and write it to log. This is preferred way for me as I already have my logging module (using log4net) in place. The only thing I need is the SQL generated by DE. Integrate DE with some logging tool. I read

Using Dapper with Oracle stored procedures which return cursors

时光毁灭记忆、已成空白 提交于 2019-11-27 12:11:17
How would one go about using Dapper with Oracle stored procedures which return cursors? var p = new DynamicParameters(); p.Add("foo", "bar"); p.Add("baz_cursor", dbType: DbType.? , direction: ParameterDirection.Output); Here, the DbType is System.Data.DbType which does not have a Cursor member. I've tried using DbType.Object but that does not work with both OracleClient and OracleDataAcess. What would be a possible way to use OracleType or OracleDbType instead? You would have to implement: public interface IDynamicParameters { void AddParameters(IDbCommand command, Identity identity); } Then

How to generate model from database using Dapper?

穿精又带淫゛_ 提交于 2019-11-27 10:36:47
问题 I am coming from PetaPoco camp. PetaPoco has a T4 template which generates model from the database. Is anything similar available for Dapper? I installed Dapper using NuGet and added SqlHelper.cs, but I didn't find anything which generates model from the database. 回答1: Dapper itself provides few extension methods (Query, Execute) for the connection object and does not have "model generator." Perhaps some other framework can be used to generate POCO's based on the db schema. Update: Database