dapper

Dynamic where clause in dapper

旧街凉风 提交于 2019-12-05 20:52:46
问题 Is it possible to add and remove criteria on the fly with dapper? I need this to implement user driven filtering. It is not feasible to have a query for each filter as there are too many combinations. 回答1: At the most basic level, you can just build the TSQL dynamically in a StringBuilder - adding extra and c.Name = @name etc . Don't worry about extra parameters; send them all - the library inspects the command and doesn't add any parameters that obviously aren't used. There is a second,

.net core dapper (2)

蹲街弑〆低调 提交于 2019-12-05 18:05:39
0.0 今天还是简答的更一篇 人生的一须尽欢,莫使金樽空对月 1. 简答的插入数据,然查询整个表 1 public static void Main(string[] args) 2 { 3 var connectionString = "Server=.;Database=Demo;User Id=sa;Password = keno;"; 4 5 //02 insert 6 var customerModel = new Customer { CustomerName = "张三", Address = "北京", City = "北京", Country = "中国", PostalCode = "121212" }; 7 using (var connection = new SqlConnection(connectionString)) 8 { 9 string sql = "insert into [dbo].[Customer]([CustomerName],[Address],[City],[PostalCode],[Country]) values(@CustomerName,@Address,@City,@PostalCode,@Country)"; 10 var addRow = connection.Execute(sql, customerModel);

How to use Dapper in ServiceStack

妖精的绣舞 提交于 2019-12-05 16:42:59
问题 Currently, I am using OrmLite for DB operations. I am also planning to use Dapper ORM, but can anyone point me how to integrate DapperORM in ServiceStack. Do I need to implement both IDbConnection and IDbConnectionFactory interfaces with Dapper and plugin into the container. public override void Configure(Container container) { container.Register<IDbConnectionFactory>( c => new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings["default"].ConnectionString, SqlServerDialect

Query spatial data with dapper

让人想犯罪 __ 提交于 2019-12-05 16:38:25
I've found some related questions , but the author gave up and went ahead with using stored procedures to do the 'mapping'. This is actually a continuation question from here Model public class Store { public int Id { get; private set; } public string Name { get; set; } public string Address { get; set; } public DbGeography Location { get; set; } } Querying using (SqlConnection conn = SqlHelper.GetOpenConnection()) { const string sql = "Select * from Stores"; return conn.Query<Store>(sql, new { Tenant_Id = tenantId }); } Dapper doesn't understand spatial data, and as many had said, it wasn't

Passing DTO to my ViewModels constructor to map properties

拥有回忆 提交于 2019-12-05 15:37:40
In my solution I have two projects. Project 1 (Core) Mapping SQL to DTO using Dapper Project 2 (WebUI - ASP.NET MVC 4) Here I use a ViewModel per View. Examples of a Controller [HttpGet] public ActionResult Edit(int id) { // Get my ProductDto in Core var product = Using<ProductService>().Single(id); var vm = new ProductFormModel(product); return View(vm); } Examples of a ViewModel public class ProductFormModel : BaseViewModel, ICreateProductCommand { public int ProductId { get; set; } public int ProductGroupId { get; set; } public string ArtNo { get; set; } public bool IsDefault { get; set; }

Can I use DynamicParameters with Template and have a return parameter in dapper?

天涯浪子 提交于 2019-12-05 15:04:42
The system I am currently working on uses Stored Procedures for all data access. I'm looking into Dapper at the moment (so far it looks great) but I was wondering if I can use a DynamicParameters object created using a Template but make one of the parameters an output param. For example: SP: CREATE PROCEDURE InsertPerson @ID int Output, @Name varchar(100), @DOB DateTime2 AS --INSERT STATEMENT SET @ID = SCOPE_IDENTITY() POCO: internal class Person { public int ID { get; set; } public string Name { get; set; } public DateTime DOB { get; set; } } Code: var procParams = new DynamicParameters

Dapper multi mapping two properties of the same type

雨燕双飞 提交于 2019-12-05 12:16:36
Let's say I have contacts stored in my database in a flattened form, such that I query them like this: SELECT Name, HomeHouseNumber, HomePostcode, WorkHouseNumber, WorkPostcode FROM Contacts I would like a little more structure in my C# code and have this simple definition of a contact with a home and work address. class Address { public string HouseNumber { get; set; } public string Postcode { get; set; } } class Contact { public string Name { get; set; } public Address HomeAddress { get; set; } public Address WorkAddress { get; set; } } I've found I can use multi mapping do extract the home

Dapper Extensions Change Schema

99封情书 提交于 2019-12-05 11:54:28
I am using Dapper Extensions to do some simple CRUD operations on a DB. My problem is that the tables I am using are held in a different schema to dbo. Is there a way to choose the schema at the dapper extensions level? or Should this be dealt with via the user that is being used to connect to the db with? You can use the AutoClassMapper to assign a new schema to your model. An overview on this is on the extensions site . You will basically need to create an AutoClassMapper per model with a different schema. The best place to declare it is alongside your model itself like: public class MyModel

Transactions in “dapper-dot-net”

蹲街弑〆低调 提交于 2019-12-05 10:59:31
How do I create a transaction if my DAL is using dapper-dot-net? My c# winform application will be used in network and the data will be saved to a central sql server. My use case requires use of transactions. Can I do this using dapper, or will I need to use something like NHibernate? Also, is there any risk or limitation with this framework if I am using stored procedures? Will I need to change my approach due any possible limitations? I haven't run into any limitations with using sprocs and the risks you have with dapper are the same risks you would have with sprocs Here is a simple example

How to get values for child objects using Dapper ORM?

喜你入骨 提交于 2019-12-05 10:50:32
I am retrieving profile details with the following: var profiles = connection.Query<Models.PROFILE>( "SELECT * FROM PROFILES WHERE ID=@ID", new { ID = profileID }); // IEnumerable var profile = profiles.First<Models.PROFILE>(); The profile object contains other collections like profileImages. The problem is that the item count for every child object is zero. Also I only want to get data for say, profileImages. Is there something that needs to be set to query the child objects, and if so, is it possible to specify which one and for how many levels? I have also tried multimapping: var profiles =