diff --git a/QueryBuilder/IgnoreInsertAttribute.cs b/QueryBuilder/IgnoreInsertAttribute.cs new file mode 100644 index 00000000..b0afc6b6 --- /dev/null +++ b/QueryBuilder/IgnoreInsertAttribute.cs @@ -0,0 +1,27 @@ +using System; + +namespace SqlKata +{ + /// + /// This class is used as metadata to ignore a property on insert queries + /// + /// + /// + /// public class Person + /// { + /// public string Name {get ;set;} + /// + /// [IgnoreInsert] + /// public string PhoneNumber {get ;set;} + /// + /// } + /// + /// new Query("Table").Insert(new Person { Name = "User", PhoneNumber = "70123456" }) + /// + /// output: INSERT INTO [Table] ([Name]) VALUES('User') + /// + /// + public class IgnoreInsertAttribute : Attribute + { + } +} diff --git a/QueryBuilder/IgnoreUpdateAttribute.cs b/QueryBuilder/IgnoreUpdateAttribute.cs new file mode 100644 index 00000000..1b7cc003 --- /dev/null +++ b/QueryBuilder/IgnoreUpdateAttribute.cs @@ -0,0 +1,27 @@ +using System; + +namespace SqlKata +{ + /// + /// This class is used as metadata to ignore a property on update queries + /// + /// + /// + /// public class Person + /// { + /// public string Name {get ;set;} + /// + /// [IgnoreUpdate] + /// public string PhoneNumber {get ;set;} + /// + /// } + /// + /// new Query("Table").Update(new Person { Name = "User", PhoneNumber = "70123456" }) + /// + /// output: UPDATE INTO [Table] ([Name]) VALUES('User') + /// + /// + public class IgnoreUpdateAttribute : Attribute + { + } +} diff --git a/QueryBuilder/Query.Update.cs b/QueryBuilder/Query.Update.cs index d88aeb00..538ef498 100644 --- a/QueryBuilder/Query.Update.cs +++ b/QueryBuilder/Query.Update.cs @@ -9,7 +9,7 @@ public partial class Query { public Query AsUpdate(object data) { - var dictionary = BuildKeyValuePairsFromObject(data, considerKeys: true); + var dictionary = BuildKeyValuePairsFromObject(data, considerKeys: true, insert: false); return AsUpdate(dictionary); } diff --git a/QueryBuilder/Query.cs b/QueryBuilder/Query.cs index 8435eca6..d95b8dcb 100755 --- a/QueryBuilder/Query.cs +++ b/QueryBuilder/Query.cs @@ -402,14 +402,16 @@ public object FindVariable(string variable) /// and will add it automatically to the Where clause /// /// - private IEnumerable> BuildKeyValuePairsFromObject(object data, bool considerKeys = false) + private IEnumerable> BuildKeyValuePairsFromObject(object data, bool considerKeys = false, bool insert= true) { var dictionary = new Dictionary(); var props = CacheDictionaryProperties.GetOrAdd(data.GetType(), type => type.GetRuntimeProperties().ToArray()); foreach (var property in props) { - if (property.GetCustomAttribute(typeof(IgnoreAttribute)) != null) + if ((property.GetCustomAttribute(typeof(IgnoreAttribute)) != null) || + (property.GetCustomAttribute(typeof(IgnoreUpdateAttribute)) != null && !insert) || + (property.GetCustomAttribute(typeof(IgnoreInsertAttribute)) != null && insert)) { continue; }