Skip to content

Commit 700dd35

Browse files
committed
Added identityInsert option in GenerateInsertWithIDAttribute #1
1 parent 2b39bcc commit 700dd35

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

Attributes/GenerateInsertWithIDAttribute.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
/// <remarks>
77
/// This attribute can be applied to classes to indicate whether the ID property's value should be included
88
/// in the insert query generation. For example, it can be used to determine whether the ID property should be
9-
/// part of the generated insert query or excluded.
9+
/// part of the generated insert query or excluded. The IncludeIdentityInsert property can contain database-specific
10+
/// statements that affect identity insert behavior (e.g., SET IDENTITY_INSERT for MSSQL), or similar statements
11+
/// applicable to other databases like MySQL or PostgreSQL.
1012
/// </remarks>
1113
[AttributeUsage(AttributeTargets.Class)]
1214
public class GenerateInsertWithIDAttribute : Attribute
@@ -17,16 +19,24 @@ public class GenerateInsertWithIDAttribute : Attribute
1719
/// <value><c>true</c> if the insert query should include the ID property; otherwise, <c>false</c>.</value>
1820
public bool GenerateWithID { get; }
1921

22+
/// <summary>
23+
/// Gets a value indicating whether to include database-specific SQL statements during insert query generation
24+
/// that affect identity insert behavior (e.g., SET IDENTITY_INSERT for MSSQL).
25+
/// </summary>
26+
/// <value><c>true</c> to include identity insert statements; otherwise, <c>false</c>.</value>
27+
public bool IncludeIdentityInsert { get; }
28+
2029
/// <summary>
2130
/// Initializes a new instance of the <see cref="GenerateInsertWithIDAttribute"/> class.
2231
/// </summary>
2332
/// <param name="generateWithID">Indicates whether the insert query generation should include the ID property.
2433
/// Default is <c>true</c>.</param>
25-
public GenerateInsertWithIDAttribute(bool generateWithID = true)
34+
/// <param name="includeIdentityInsert">Indicates whether to include identity insert statements in the
35+
/// insert query generation. Default is <c>true</c>.</param>
36+
public GenerateInsertWithIDAttribute(bool generateWithID = true, bool includeIdentityInsert = true)
2637
{
2738
GenerateWithID = generateWithID;
39+
IncludeIdentityInsert = includeIdentityInsert;
2840
}
29-
30-
3141
}
32-
}
42+
}

Helper/QueryHelper.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ public bool GetInsertWithID<T>() where T : IDataContractComparer
6969
return false;
7070
}
7171

72+
/// <summary>
73+
/// Gets whether the type specifies to include database-specific SQL statements for identity insert behavior
74+
/// during insert query generation, considering the GenerateInsertWithIDAttribute if present.
75+
/// </summary>
76+
/// <typeparam name="T">The type for which to determine the inclusion of identity insert statements.
77+
/// Must implement <see cref="IDataContractComparer"/>.</typeparam>
78+
/// <returns><c>true</c> if identity insert statements should be included; otherwise, <c>false</c>.</returns>
79+
public bool GetIncludeIdentityInsert<T>() where T : IDataContractComparer
80+
{
81+
GenerateInsertWithIDAttribute? attribute = (GenerateInsertWithIDAttribute?)Attribute.GetCustomAttribute(typeof(T), typeof(GenerateInsertWithIDAttribute));
82+
83+
if (attribute != null)
84+
return attribute.IncludeIdentityInsert;
85+
86+
return false;
87+
}
88+
7289
/// <summary>
7390
/// Gets the names of properties marked as key columns for a specified type.
7491
/// </summary>

0 commit comments

Comments
 (0)