dapper

Dapper - Insert using nested object

本小妞迷上赌 提交于 2019-12-06 13:55:56
问题 I have a class User with a nested class Bank . class User{ int Id; string username; Bank bank; } Class Bank{ int id; string name; } I need to create an Insert function for User . Is there a way in Dapper to execute a query and binding the parameters from a nested a object ? 回答1: You can write a custom mapper for Dapper using DapperExtensions : public sealed class UserMapper : ClassMapper<User> { public UserMapper() { Map(x => x.bank.id).Column("BankId"); Map(x => x.bank.name).Column("BankName

Select All Posts With All Their Tags

女生的网名这么多〃 提交于 2019-12-06 12:13:09
There are many questions about this, but not enough definitive answers, especially using SQL Server which I am in this case. I have 3 tables to represent blog posts and tags associated with it. I want to run a query that will get all the posts and each posts tags with it - I need both the tag id and name. The result needs to be easily serializable into my C# classes. I am also trying to use Dapper.net but that is not the most important part. Obviously many websites do this, I want to know the best way and how it should be done in the real world? I could get all the posts, then run multiple

Why does Dapper QueryAsync<T> behave differently from Query<T> with sproc return values?

混江龙づ霸主 提交于 2019-12-06 12:09:19
问题 I have a stored procedure that performs a check, and returns either a row or a non-zero return value: CREATE PROCEDURE dbo.ConditionalGet @ID INT AS BEGIN -- this is a silly made up condition just to test the issue IF @ID % 2 = 1 BEGIN RETURN -1 END SELECT * FROM MyTable WHERE ID = @ID RETURN 0 END And I have a C# repo class that uses Dapper to return the result or throw an exception: public class Repo { public MyClass Get(int id) { using (var conn = GetSqlConnection()) { var p = new { ID =

return a list of data via stored proc to dapper

房东的猫 提交于 2019-12-06 09:51:34
I am trying to return data using Dapper via stored proc My DTO Class is similar to below (removed some properties for brevity) public class CarDTO { public int CarID { get; set; } public string Manufacturer { get; set; } public List<CarOptionDTO> CarOptions { get; set; } } So basically in the DB I have a CarOption table that has a CarID column - i.e a Car can have many options. My DAL Layer call at the minute is as below: private string getCarDataSp = "[dbo].[GetCarData]"; public IEnumerable<CarDTO> GetCarData(int customerId, int year) { return Get(db => db.Query<CarDTO>(getCarDataSp , new {

Table Value Parameter with Dapper stored procedures

冷暖自知 提交于 2019-12-06 07:52:24
问题 I am attempting to call a stored procedure that accepts a table valued parameter. I am following guidelines on this question, implementing a custom parameter type: internal class IntDynamicParam { string name; IEnumerable<int> numbers; public IntDynamicParam(string name,IEnumerable<int> numbers) { this.name = name; this.numbers = numbers; } public void AddParameters(IDbCommand command) { var sqlCommand = (SqlCommand)command; sqlCommand.CommandType = CommandType.StoredProcedure; List<Microsoft

Dapper: Mapping dynamic pivot columns from stored procedure

血红的双手。 提交于 2019-12-06 05:55:57
问题 My stored procedure is returning dynamic pivot columns as available. I am using the SQLMapper and COlumnTypeAttribute, But in the results I can only see the first column and its value, but the dynamic pivot column(s) and their values(s) are empty. the sample data could look like The first column is fixed, Rest of the columns are pivot columns. TSBNumber SystemXY SystemBB SystemTT asdas 1/1/2013 1231 1/1/2014 12312 1/1/2013 ASAWS 1/1/2013 awsdS 1/1/2013 Store Procedure DECLARE

Populating POCO's with ServiceStack.OrmLite

别来无恙 提交于 2019-12-06 05:01:01
Consider the following domain model: class Customer { int id {get;set} string Name {get;set} List<Contact> Contacts {get;set} } class Contact { int id {get;set} string FullName {get;set} List<PhoneNumber> {get;set} } class PhoneNumber { int id {get;set} PhoneType Type {get;set} string Number {get;set} } Customer, Contact & PhoneNumbers are separate entities in our DB. Any Suggestions as to how to populate a full customer with all its linked contacts and the contacts phone numbers in the most efficient way using ORMLite and/or Dapper, with consideration of calls to the db and cpu cycles to map

How do I build a dynamic sql query with Dapper.SqlBuilder and OrWhere

泪湿孤枕 提交于 2019-12-06 04:12:07
问题 I am attempting to build a dynamic Sql query for multiple search terms. I understand in general how to use the builder, but am not sure what to do in the loop since I actually need the @term to be different each time (I think). Not just in the query, but in the anonymous type as well to match. I could use a string.Format in the query string, but not sure how to match it in the anonymous type? public async Task<List<Thing>> Search(params string[] searchTerms) { var builder = new SqlBuilder();

Dapper Spatial Geography Type

梦想与她 提交于 2019-12-06 03:28:01
I'm cooking up some spatial examples and have decided to give Dapper a go, although EF has spatial support, and I'm loving having control over my SQL again (Thanks Sam & Marc). However, I need to be able to have POCO's that support the DbGeography class. For example : public class BuriedTreasure { public int PirateId { get; set; } public DbGeography MarksTheSpot { get; set; } } My Google foo has been letting me down and the closest match I can find is this question, though it only caters for adding spatial support as a parameter (so it's 50% there). Now as far as I can see I'm limited to the

Filling one to many relationship using using Dapper or via Linq

a 夏天 提交于 2019-12-06 03:08:32
问题 Entity - AllSalesTerritory contains List<MySalesPerson> representing one to many relationship. I have Sql query to fetch the data where the two entities are mapped using a column TerritoryId . I use a following code to fill the entity using Dapper micro ORM : List<AllSalesTerritory> allSalesTerrotories = _connection.Query<AllSalesTerritory, MySalesPerson, AllSalesTerritory> (query, (pd, pp) => { pd.SalesPersons.Add(pp); return pd; }, splitOn: "BusinessEntityId") .ToList(); BusinessEntityId is