dapper

Dapper MultiMap doesn't work with splitOn with NULL value

人盡茶涼 提交于 2019-12-03 15:27:36
问题 I have a problem with MultiMaps in dapper trying to split on column that contains NULL . Dapper seems not to instantiate object and my mapping function receives null instead of object. Here's my new test: class Product { public int Id { get; set; } public string Name { get; set; } public Category Category { get; set; } } class Category { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } } public void TestMultiMapWithSplitWithNullValue() { var

Simple but good example on how to use Dapper with Structuremap and dependency injection

吃可爱长大的小学妹 提交于 2019-12-03 15:00:35
I am trying to understand how to use Dependency Injection with Dapper (IDbConnection) and still being able to use built in dispose. I have found a couple of articles on the web but non that I think is easy to understand. What I am trying to figure out is how to make this simple class be testable: public class UserProfileRepository : IUserProfileRepository { private readonly IConfigRepository _configRepository; public UserProfileRepository(IConfigRepository configRepository) { _configRepository = configRepository; } public UserProfile GetUserProfile(string userId) { const string query = @

Does Dapper support SQL 2008 Table-Valued Parameters 2?

北慕城南 提交于 2019-12-03 14:13:51
I know that dapper can support TVF, but how do you send extra parameters along with TVF (without adding it to the IntDynamicParam class)? See the below example from Tests.cs, i have modified to add the extra parameter: connection.Execute("CREATE TYPE int_list_type AS TABLE (n int NOT NULL PRIMARY KEY)"); connection.Execute("CREATE PROC get_ints @x int, @ints int_list_type READONLY AS select * from @ints"); I tried the following but got errors (No mapping exists from object type SqlMapper.Tests+IntDynamicParam to a known managed provider native type.): var p = new DynamicParameters(); p.Add("x"

Is AsList() better than ToList() with IDbConnection.Query() which returns IEnumerable?

我与影子孤独终老i 提交于 2019-12-03 12:49:39
I read this answer from Marc Gravell (@MarcGravell): https://stackoverflow.com/a/47790712/5779732 The last line says: As a minor optimization to your code: prefer AsList() to ToList() to avoid creating a copy. That statement is about QueryMultiple() which returns GridReader . In my understanding, System.Linq provides an extension method IEnumerable.ToList() . Following is from Microsoft about ToList() . The ToList(IEnumerable) method forces immediate query evaluation and returns a List that contains the query results. You can append this method to your query in order to obtain a cached copy of

Dapper Multi-map next level

时光怂恿深爱的人放手 提交于 2019-12-03 12:34:54
I'm using multiple mapping for a current query and now I need to map another object on the initial query. For example: public class Part { public int Id { get; set; } public string Name { get; set; } public Address Address { get; set; } } public class Address { public int Id { get; set; } public string Street { get; set; } public SiteOu Ou { get; set; } } public class SiteOu public int Id { get; set; } public string Name { get; set; } } Dapper: connection.Query<Part, Address, Part>(sql, (part, address) => { part.Address = address; }); How do I get the Address class to have the SiteOu

Dapper with .NET Core - injected SqlConnection lifetime/scope

拥有回忆 提交于 2019-12-03 12:27:25
I'm using .NET Core Dependency Injection to instantiate a SqlConnection object during the application startup, which I'm then planning to inject in my repository. This SqlConnection will be used by Dapper to read/write data from the database within my repository implementation. I am going to use async calls with Dapper. The question is: should I inject the SqlConnection as transient or as a singleton? Considering the fact that I want to use async my thought would be to use transient unless Dapper implements some isolation containers internally and my singleton's scope will still be wrapped

Dapper.NET and IQueryable

喜你入骨 提交于 2019-12-03 11:53:36
问题 Is there a plan to make Dapper.net compatible with IQueryable interfaces? If not, what's the workaround to use Dapper with "Expression Trees" filters? 回答1: No, there are no plans to do this. It is far far outside what dapper tries to do. So far that I would say it is antithetical. Dapper core tries to be the friend to those who love their SQL. 回答2: You can get IQueryable from IEnumerable using the built-in extension method AsQueryable in System.Linq namespace public IQueryable<Order>

Call stored procedure from dapper which accept list of user defined table type

有些话、适合烂在心里 提交于 2019-12-03 11:21:28
问题 I have a stored procedure InsertCars which accepts list of user defined table type CarType . CREATE TYPE dbo.CarType AS TABLE ( CARID int null, CARNAME varchar(800) not null, ); CREATE PROCEDURE dbo.InsertCars @Cars AS CarType READONLY AS -- RETURN COUNT OF INSERTED ROWS END I need call this stored procedure from Dapper. I googled it and found some solutions. var param = new DynamicParameters(new{CARID= 66, CARNAME= "Volvo"}); var result = con.Query("InsertCars", param, commandType:

How to pass a null parameter with Dapper

杀马特。学长 韩版系。学妹 提交于 2019-12-03 10:32:29
问题 I have a stored procedure that has a parameter with no default value, but it can be null. But I can't figure out how to pass null with Dapper. I can do it just fine in ADO. connection.Execute("spLMS_UpdateLMSLCarrier", new { **RouteId = DBNull.Value**, CarrierId = carrierID, UserId = userID, TotalRouteRateCarrierId = totalRouteRateCarrierId }, commandType: CommandType.StoredProcedure); Exception: System.NotSupportedException was caught Message=The member RouteId of type System.DBNull cannot

How do I perform an insert and return inserted identity with Dapper?

匿名 (未验证) 提交于 2019-12-03 08:59:04
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How do I perform an insert to database and return inserted identity with Dapper? I've tried something like this: string sql = "DECLARE @ID int; " + "INSERT INTO [MyTable] ([Stuff]) VALUES (@Stuff); " + "SELECT @ID = SCOPE_IDENTITY()"; var id = connection.Query (sql, new { Stuff = mystuff}).First(); But it did't work. @Marc Gravell thanks, for reply. I've tried your solution but, still same exception trace is below System.InvalidCastException: Specified cast is not valid at Dapper.SqlMapper. d__a`1.MoveNext() in (snip)\Dapper\SqlMapper.cs