Repository navigation
Home
This version of Dapper Extensions has been adjusted to add a library for DNX 1.0.0, so you can use it (along with the latest version of Dapper) in your .Net Core projects.
Tim Smith's (brilliant) original Dapper-Extensions library hasn't been adjusted in a few years, so we've incorporated most of the pull requests from that library into this update as well.
While the readme.md file has not been materially adjusted and remains very close to what exists in the original project, these wiki pages have been added/adjusted to include DNX documentation as well as new features and more information about how to use predicates.
- Zero configuration required out of the box (just select a SQL dialect and use the extensions).
- Automatic mapping of POCOs for Get, Insert, Update, and Delete operations.
- GetList, Count methods for more advanced scenarios.
- GetPage for returning paged result sets.
- Automatic support for Guid and Integer keys primary keys (Includes manual support for other key types).
- Mapped via ClassMapper without use of Attributes, but also supports attribute-based mapping if that's the approach you'd like to take.
- Support for Composite Primary Keys.
- Singular and Pluralized table name support (eg - Person objects can be saved to a "Person" table or a "Persons" table automatically).
- Predicate-based queries let you eliminate SQL from your repositories in even more advanced scenarios (eg - queries that are based on fields other than your primary key).
- Properly escaped table/column names in generated SQL (Ex: SELECT [FirstName] FROM [Users] WHERE [Users].[UserId] = @UserId)
- DNX-driven Unit test coverage (all of the original Dapper-Extension unit tests have been ported to DNX)
- POCO names should match the table name in the database (note that Pluralized table names are supported through the PlurizedAutoClassMapper).
- POCO property names should match each table column name.
- By convention, the extension assumes your primary key is named Id, but you can map your key to another name via custom mappings.
For more information, please view our Getting Started guide.
http://nuget.org/List/Packages/DapperExtensions-dnx
PM> Install-Package DapperExtensions-dnx
The following examples will use a Person POCO defined as:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Active { get; set; }
public DateTime DateCreated { get; set; }
}using (IDBConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
int personId = 1;
Person person = cn.Get<Person>(personId);
}using (IDBConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
Person person = new Person { FirstName = "Foo", LastName = "Bar" };
int id = cn.Insert(person);
}public class Car
{
public int ModelId { get; set; }
public int Year { get; set; }
public string Color { get; set; }
}
...
using (IDBConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
Car car = new Car { Color = "Red" };
var multiKey = cn.Insert(car);
int modelId = multiKey.ModelId;
int year = multiKey.Year;
}using (IDBConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
int personId = 1;
Person person = cn.Get<Person>(personId);
person.LastName = "Baz";
cn.Update(person);
}using (IDBConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
Person person = cn.Get<Person>(1);
cn.Delete(person);
}using (IDBConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
var predicate = Predicates.Field<Person>(f => f.Active, Operator.Eq, true);
IEnumerable<Person> list = cn.GetList<Person>(predicate);
}Generated SQL
SELECT
[Person].[Id]
, [Person].[FirstName]
, [Person].[LastName]
, [Person].[Active]
, [Person].[DateCreated]
FROM [Person]
WHERE ([Person].[Active] = @Active_0)
More information on predicates can be found in our wiki.
using (IDBConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
var predicate = Predicates.Field<Person>(f => f.DateCreated, Operator.Lt, DateTime.UtcNow.AddDays(-5));
int count = cn.Count<Person>(predicate);
cn.Close();
} Generated SQL
SELECT
COUNT(*) Total
FROM [Person]
WHERE ([Person].[DateCreated] < @DateCreated_0)
More information on predicates can be found in our wiki.
Copyright 2011 Thad Smith, Page Brooks and contributors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.