Skip to content

Commit 179cb88

Browse files
committed
Improved primary key column name referencing in the SqlKataRepository.cs
1 parent df7a953 commit 179cb88

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Repository/SqlKataRepository.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
43
using Database;
@@ -44,21 +43,26 @@ public SqlKataRepository(SqlKataDatabase db, string tableName)
4443
/// <returns>ID of the given object</returns>
4544
protected abstract int GetEntryId(T entry);
4645

46+
/// <summary>
47+
/// Returns the name of the primary key column
48+
/// </summary>
49+
protected string GetPrimaryKeyColumnName => SqlSchema.TableIdPrimaryKey;
50+
4751
// use the InsertGetId method of the SqlKata library in order to insert a new row and get it's ID
4852
public int Add(T entry) => Db.Get().Query(TableName).InsertGetId<int>(ToRow(entry));
4953

5054
// updates the given object of type T using the WHERE and UPDATE method calls, returns a boolean flag indicating
5155
// whether at least one table row was updated
5256
public bool Update(T entry) =>
53-
Db.Get().Query(TableName).Where(SqlSchema.TableIdPrimaryKey, GetEntryId(entry)).Update(ToRow(entry)) > 0;
57+
Db.Get().Query(TableName).Where(GetPrimaryKeyColumnName, GetEntryId(entry)).Update(ToRow(entry)) > 0;
5458

5559
// deletes the given object of type T and returns a boolean flag indicating whether at least one table row was deleted
56-
public bool Delete(T entry) => Db.Get().Query(TableName).Where(SqlSchema.TableIdPrimaryKey, GetEntryId(entry)).Delete() > 0;
60+
public bool Delete(T entry) => Db.Get().Query(TableName).Where(GetPrimaryKeyColumnName, GetEntryId(entry)).Delete() > 0;
5761

5862
public T Get(int id)
5963
{
6064
// find a table rows based on the given ID
61-
var result = Db.Get().Query(TableName).Where(SqlSchema.TableIdPrimaryKey, id).FirstOrDefault();
65+
var result = Db.Get().Query(TableName).Where(GetPrimaryKeyColumnName, id).FirstOrDefault();
6266

6367
// convert the given table row a ta an object of type T
6468
return result == null ? default : FromRow(result);

0 commit comments

Comments
 (0)