Skip to content

Commit

Permalink
Implemented NotMapped feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Тембеков Арсений committed Feb 13, 2019
1 parent 5ac5cd3 commit b05ee76
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public MyClass(int id, int score, string winner)
Foo = "Maps to bar!";
}

[NotMapped]
public string Winner { get; private set; }
public int Score { get; set; }
public int Id { get; set; }
Expand Down
17 changes: 16 additions & 1 deletion Logic/Common.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;

namespace System.Data.SqlClient
Expand All @@ -13,6 +12,8 @@ public static DataTable GetDataTableFromFields<T>(IEnumerable<T> data, SqlBulkCo
Type listType = typeof (T);
foreach (PropertyInfo propertyInfo in listType.GetProperties())
{
if(IsColumnNotMapped(propertyInfo)) continue;

var columnName = GetColumnName(propertyInfo);
dt.Columns.Add(columnName, propertyInfo.PropertyType);
sqlBulkCopy.ColumnMappings.Add(columnName, columnName);
Expand All @@ -23,6 +24,8 @@ public static DataTable GetDataTableFromFields<T>(IEnumerable<T> data, SqlBulkCo
DataRow dr = dt.NewRow();
foreach (PropertyInfo propertyInfo in listType.GetProperties())
{
if (IsColumnNotMapped(propertyInfo)) continue;

var columnName = GetColumnName(propertyInfo);
dr[columnName] = propertyInfo.GetValue(value, null);
}
Expand Down Expand Up @@ -50,5 +53,17 @@ public static string GetColumnName(PropertyInfo propertyInfo)
//it doesn't exist so return the property name
return propertyInfo.Name;
}

/// <summary>
/// Check if property is should be mapped
/// If the System.ComponentModel.DataAnnotations.NotMappedAttribute is used
/// it will skip this property
/// </summary>
/// <param name="propertyInfo"></param>
/// <returns></returns>
public static bool IsColumnNotMapped(PropertyInfo propertyInfo)
{
return propertyInfo.GetCustomAttribute<NotMappedAttribute>(false) != null;
}
}
}

0 comments on commit b05ee76

Please sign in to comment.