dapper-extensions

Create a DbContext that handle a DatabaseFactory to use DapperExtensions more easily

纵饮孤独 提交于 2019-12-20 04:10:21
问题 This days I try to create an abstract base repository using some basic CRUD functions proposed by DapperExtensions. But the code given as an exemple use a SqlConnection which is made to connect to a SQL Server database. I want to be able to connect to all kind of Database (SQL Server, MySql, etc...). Also their code sample is repeated for each CRUD function as the code below show using (SqlConnection cn = new SqlConnection(_connectionString)) { cn.Open(); //Code doing something here... cn

Dapper - Bulk insert of new items and get back new IDs

这一生的挚爱 提交于 2019-12-12 09:57:18
问题 I am using dapper to add multiple new students in one db hit using this method: db.ExecuteAsync(@"INSERT Student(Name,Age) values (@Name,@Age)", students.Select(s => new { Name = s.Name, Age = s.Age }) ); But the problem I don't have the new ids. Can I make one db hit and still some how get the new ids ? And if not, what is the most efficient way of performing such bulk insert ? 回答1: That is not a bulk insert; it is basically just shorthand that unrolls the loop; although interestingly

Dapper.SimpleCRUD Insert / Update / Get fails with message “Entity must have at least one [Key] property”

假装没事ソ 提交于 2019-12-10 13:33:40
问题 I'm a new baby in Dapper. Trying to incorporate CRUD operations with Dapper and Dapper.SimpleCRUD lib. Here is the sample code... My Data Model Looks like Class Product { public string prodId {get;set;} public string prodName {get;set;} public string Location {get;set;} } Dapper Implementation - Insert public void Insert(Product item) { using(var con = GetConnection()) { con.Insert(item); } } Since the ProdId in the Db is an Identity Column it fails. How does it indicate ProdId is an Identity

Multi-Mapper in Dapper one to many relations

青春壹個敷衍的年華 提交于 2019-12-10 11:51:03
问题 I am trying to get the value of my database with a relation one to many i have my object like this Student [Table("Student")] public class Student : IStudent { public int Id { get; set; } public string Lastname { get; set; } public string FirstMidName { get; set; } public DateTime? EnrollmentDate { get; set; } [Write(false)] public IEnumerable<Enrollment> Enrollments { get; set; } } Enrollment [Table("Enrollment")] public class Enrollment { public int Id { get; set; } public int CourseId {

Querying into a complex object with Dapper

大兔子大兔子 提交于 2019-12-08 17:20:53
问题 I have a Customer class with the following properties: public int Id { get; set; } public string Name { get; set; } public int AddressId { get; set; } public Address Address { get; set; } My goal is to write a Dapper query that will use an Inner Join to populate the entire Address property within each Customer that is returned. Here is what I have and it is working but I am wondering if this is the cleanest/simplest way to do it: StringBuilder sql = new StringBuilder(); using (var conn =

Dapper Extensions Change Schema

假如想象 提交于 2019-12-07 06:24:22
问题 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? 回答1: 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

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

Create a DbContext that handle a DatabaseFactory to use DapperExtensions more easily

亡梦爱人 提交于 2019-12-02 06:23:58
This days I try to create an abstract base repository using some basic CRUD functions proposed by DapperExtensions . But the code given as an exemple use a SqlConnection which is made to connect to a SQL Server database. I want to be able to connect to all kind of Database (SQL Server, MySql, etc...). Also their code sample is repeated for each CRUD function as the code below show using (SqlConnection cn = new SqlConnection(_connectionString)) { cn.Open(); //Code doing something here... cn.Close(); } So i was thinking about to create a DbContext that can handle the creation, the opening and

Querying into a complex object with Dapper

江枫思渺然 提交于 2019-12-01 16:50:28
I have a Customer class with the following properties: public int Id { get; set; } public string Name { get; set; } public int AddressId { get; set; } public Address Address { get; set; } My goal is to write a Dapper query that will use an Inner Join to populate the entire Address property within each Customer that is returned. Here is what I have and it is working but I am wondering if this is the cleanest/simplest way to do it: StringBuilder sql = new StringBuilder(); using (var conn = GetOpenConnection()) { sql.AppendLine("SELECT c.Id, c.Name, c.AddressId, a.Address1, a.Address2, a.City, a

Ignore property on model property

拈花ヽ惹草 提交于 2019-11-29 13:23:05
How can I ignore a property on my model using dapper/dapper extensions/dapper rainbow or any of those dapper libraries? Shiva Dapper creator Sam Saffron has addressed this requirement in response to another SO user's questions here . Check it out. Also, if you want to use the Dapper Extensions library that Sam has mentioned in his answer, you can get it from Github or via Nuget. Here's an example of ignoring properties from the Library's Test Project . using System; using System.Collections.Generic; using DapperExtensions.Mapper; namespace DapperExtensions.Test.Data { public class Person {