Skip to content

Commit

Permalink
Adds support for column attribute mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray committed Jun 19, 2015
1 parent 45dcbc0 commit 0ecaf72
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Demo/CreateSampleTable.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
-- sample table schema
-- sample table schema
CREATE TABLE [dbo].[TableName](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Score] [int] NULL,
[Winner] [varchar](100) NULL,
[Bar] [varchar](100) NULL,
[CreatedOn] [datetime] NULL,
[IsFinal] [bit] NOT NULL CONSTRAINT [DF_TableName_IsFinal] DEFAULT ((0)),
CONSTRAINT [PK_TableName] PRIMARY KEY CLUSTERED
Expand Down
1 change: 1 addition & 0 deletions Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
5 changes: 5 additions & 0 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.SqlClient;
using System.Linq;

Expand Down Expand Up @@ -40,18 +41,22 @@ public class MyClass
{
public MyClass()
{
Foo = "Maps to bar!";
}

public MyClass(int id, int score, string winner)
{
Winner = winner;
Id = id;
Score = score;
Foo = "Maps to bar!";
}

public string Winner { get; private set; }
public int Score { get; set; }
public int Id { get; set; }
public bool IsFinal { get; set; }
[Column("Bar")]
public string Foo { get; set; }
}
}
30 changes: 27 additions & 3 deletions Logic/Common.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;

namespace System.Data.SqlClient
Expand All @@ -11,20 +13,42 @@ public static DataTable GetDataTableFromFields<T>(IEnumerable<T> data, SqlBulkCo
Type listType = typeof (T);
foreach (PropertyInfo propertyInfo in listType.GetProperties())
{
dt.Columns.Add(propertyInfo.Name, propertyInfo.PropertyType);
sqlBulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name);
var columnName = GetColumnName(propertyInfo);
dt.Columns.Add(columnName, propertyInfo.PropertyType);
sqlBulkCopy.ColumnMappings.Add(columnName, columnName);
}

foreach (T value in data)
{
DataRow dr = dt.NewRow();
foreach (PropertyInfo propertyInfo in listType.GetProperties())
{
dr[propertyInfo.Name] = propertyInfo.GetValue(value, null);
var columnName = GetColumnName(propertyInfo);
dr[columnName] = propertyInfo.GetValue(value, null);
}
dt.Rows.Add(dr);
}

return dt;
}

/// <summary>
/// Gets the column name for the target database.
/// If the System.ComponentModel.DataAnnotations.ColumnAttribute is used
/// it will attempt to use this value as the target column name.
/// </summary>
/// <param name="propertyInfo"></param>
/// <returns></returns>
public static string GetColumnName(PropertyInfo propertyInfo)
{
//check first for the DataAnnotations.ColumnAttribtue
var columnAttribute = propertyInfo.GetCustomAttribute<ColumnAttribute>(false);

if (columnAttribute != null) //it exists so use the attr value
return columnAttribute.Name;

//it doesn't exist so return the property name
return propertyInfo.Name;
}
}
}
1 change: 1 addition & 0 deletions Logic/Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ using (var ssbc = new SimpleSqlBulkCopy("ConnectionString"))
}
```
where `T` is an object of the same Properties (and Type of the Database)

If the name of the column on the target database does match the name of the property
on your call you can create the mapping by using the `Column` attribute from
`System.ComponentModel.DataAnnoations`. An example of this can be seen in the Demo program
where the property of "Foo" on the `MyClass` class is mapped to the database column of `Bar`.

0 comments on commit 0ecaf72

Please sign in to comment.