From b05ee76de98ed19ff5e5dc8854039c7021d2907e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B5=D0=BC=D0=B1=D0=B5=D0=BA=D0=BE=D0=B2=20=D0=90?= =?UTF-8?q?=D1=80=D1=81=D0=B5=D0=BD=D0=B8=D0=B9?= Date: Wed, 13 Feb 2019 12:52:18 +0300 Subject: [PATCH] Implemented NotMapped feature --- Demo/Program.cs | 1 + Logic/Common.cs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Demo/Program.cs b/Demo/Program.cs index eeeb474..404b30c 100644 --- a/Demo/Program.cs +++ b/Demo/Program.cs @@ -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; } diff --git a/Logic/Common.cs b/Logic/Common.cs index fc1ef2d..ce06575 100644 --- a/Logic/Common.cs +++ b/Logic/Common.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; using System.Reflection; namespace System.Data.SqlClient @@ -13,6 +12,8 @@ public static DataTable GetDataTableFromFields(IEnumerable 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); @@ -23,6 +24,8 @@ public static DataTable GetDataTableFromFields(IEnumerable 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); } @@ -50,5 +53,17 @@ public static string GetColumnName(PropertyInfo propertyInfo) //it doesn't exist so return the property name return propertyInfo.Name; } + + /// + /// Check if property is should be mapped + /// If the System.ComponentModel.DataAnnotations.NotMappedAttribute is used + /// it will skip this property + /// + /// + /// + public static bool IsColumnNotMapped(PropertyInfo propertyInfo) + { + return propertyInfo.GetCustomAttribute(false) != null; + } } } \ No newline at end of file