dapper

Simple inner join result with Dapper?

爷,独闯天下 提交于 2019-11-28 05:18:12
I can't seem to find documentation or examples for my problem (been searching a while now). I think my problem is pretty straightforward, so here goes. I have two tables. My primary table is called Persons and the secondary table is PersonEntries. For each person in Person table, i can have 0 or more entries in the PersonEntries table. Like this. Table: Person Id Name Table: PersonEntry PersonId CheckinTime CheckoutTime I have two objects like this public class Person { public string Name; public List<PersonEntry> PersonEntries; } public class PersonEntry { public DateTime CheckinTime; public

How to insert a C# List to database using Dapper.NET

假如想象 提交于 2019-11-28 04:49:21
Using dapper , how can I insert a C# List to database. Previously without dapper I used the below code to insert the List values to database . try { connection.Open(); for (int i = 0; i < processList.Count; i++) { string processQuery = "INSERT INTO PROCESS_LOGS VALUES (@Id, @st_Time, @ed_Time, @td_Time)"; command = new SqlCommand(processQuery, connection); command.Parameters.Add("Id", SqlDbType.Int).Value = processList[i].ID; command.Parameters.Add("st_Time", SqlDbType.DateTime).Value = processList[i].ST_TIME; command.Parameters.Add("ed_Time", SqlDbType.DateTime).Value = processList[i].ED_TIME

Explanation of dapper buffer/cache

跟風遠走 提交于 2019-11-28 04:40:16
I use dapper to return objects from my database as IEnumerable. As default dapper has buffer setting set to true. How does this work? If dapper cache the first query and then get the objects from memory. What happens if someone edit/delete/add rows in the table. Must dapper recache all data again for this query? The buffer is unrelated to cache. Dapper does not include any kind of data-cache (although it does have a cache related to how it processes commands, i.e. "this command string, with this type of parameter, and this type of entity - has these associated dynamically generated methods to

How to return null from a Dapper query rather than default(T)?

六眼飞鱼酱① 提交于 2019-11-28 04:03:58
问题 I'm using Dapper for some read-only database calls via a stored procedure. I've got a query that will either return 1 row or nothing. I'm using Dapper like this: using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); return conn.Query<CaseOfficer>("API.GetCaseOfficer", new { Reference = reference }, commandType: CommandType.StoredProcedure).FirstOrDefault(); } The returned CaseOfficer object looks like this: public class CaseOfficer { public string Title { get; set; } public

How to create arguments for a Dapper query dynamically

老子叫甜甜 提交于 2019-11-28 03:22:27
I have a dictionary of values Eg "Name": "Alex" Is there a way to pass this to Dapper as arguments for a query? Here is an example showing what I want to do. IDictionary<string, string> args = GetArgsFromSomewhere(); string query = "select * from people where Name = @Name"; var stuff = connection.Query<ExtractionRecord>(query, args); Yes: var dbArgs = new DynamicParameters(); foreach(var pair in args) dbArgs.Add(pair.Key, pair.Value); Then pass dbArgs in place of args : var stuff = connection.Query<ExtractionRecord>(query, dbArgs); Alternatively, you can write your own class that implements

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

[亡魂溺海] 提交于 2019-11-28 02:49:00
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 Access strategy? I created extension methods with a property that retrieves the connection string from

全链路设计与实践

倾然丶 夕夏残阳落幕 提交于 2019-11-28 00:34:47
背景 随着公司业务的高速发展,公司服务之间的调用关系愈加复杂,如何理清并跟踪它们之间的调用关系就显的比较关键。线上每一个请求会经过多个业务系统,并产生对各种缓存或者 DB 的访问,但是这些分散的数据对于问题排查,或者流程优化提供的帮助有限。在这样复杂的业务场景下,业务流会经过很多个微服务的处理和传递,我们难免会遇到这些问题: 一次请求的流量从哪个服务而来? 最终落到了哪个服务中去? 为什么这个请求这么慢? 到底哪个环节出了问题? 这个操作需要依赖哪些东西? 是数据库还是消息队列? Redis挂了,哪些业务受影响? 设计目标 低消耗性:跟踪系统对业务系统的影响应该做到足够小。在一些高度优化过的服务,即使一点点损耗也容易察觉到,而且有可能迫使在线负责的部署团队不得不将跟踪系统关停 低侵入性:作为非业务组件,应当尽可能少侵入或者无侵入业务系统,对于使用方透明,减少开发人员的负担 时效性:从数据的收集产生,到数据计算处理,再到最终展现,都要求尽可能快 决策支持:这些数据是否能在决策支持层面发挥作用,特别是从 DevOps 的角度 数据可视化:做到不用看日志通过可视化进行筛选 实现功能 故障定位:调用链路跟踪,一次请求的逻辑轨迹可以完整清晰的展示出来。 性能分析:调用链的各个环节分别添加调用耗时,可以分析出系统的性能瓶颈,并针对性的优化。 数据分析:调用链是一条完整的业务日志

“WHERE x IN y” clause with dapper and postgresql throwing 42601: syntax error at or near \“$1\”

对着背影说爱祢 提交于 2019-11-27 23:14:09
问题 I have an array of strings, and I'd like to have a query containing an IN clause, like: "... WHERE t.name IN ('foo', 'bar', 'baz')..>" Here's the final bit of my query, which contains a "where X in Y" clause: ... left join genre_tag_band_join tj on hb.id = tj.band_id or ob.id = tj.band_id left join genre_tags t on tj.genre_tag_id = t.id inner join venues v on e.venue_id = v.id where t.name IN @tagsParam... I make a Dapper call like this var shows = con.Query<Event, Band, Band, GenreTag, Venue

How to return dynamic types List<dynamic> with Dapper ORM

纵然是瞬间 提交于 2019-11-27 20:28:41
问题 I have been using Dapper.net for a while now and its a very good ORM mapper which works great with .Net dynamic types. But I noticed that when Dapper retrieves data from a database it returns as DapperRow type. Is there are any way that I can return it in any other type Like System.Dynamic.ExpandoObject ? 回答1: The DapperRow object is designed to share a lot of state between rows. For example, if you fetch 40 rows, the column names etc are only stored once . If we used ExpandoObject , this

Dapper and SQL Injections

[亡魂溺海] 提交于 2019-11-27 19:17:16
How does Dapper help protect against SQL injections? I am testing out different DAL technologies and have to choose one to be secure our site. I'm leaning towards Dapper (http://code.google.com/p/dapper-dot-net/), but need some help learning about security. How does Dapper help protect against SQL injections? It makes it really, really easy to do fully parameterized data access, without ever needing to either concatenate input. In particular, because you don't need to jump through lots of "add parameter, set the parameter type, check for null because ADO.NET has sucky null-handling , rinse