C
C#12mo ago
Camster

❔ ✅ Use Reflection or Generics to specify one or more properties

I'm writing something that will automatically create a table and fill it with data based on the properties of a generic object. It's for unit testing, to be built and blown away when finished, so I don't need a heavy-duty framework like EF Core. Hopefully the code below makes sense, without all the details. I'm just looping through the properties and building Create Table and Insert sql statements.
public class Table<T> {
public string GetSql_CreateTable() {
var sql = new StringBuilder();
foreach (var prop in typeof(T).GetProperties()) {
// generate create table sql
}
return sql.ToString();
}

public string Fill(List<T> rows) {
var sql = new StringBuilder();
// generate insert sql
return sql.ToString();
}
}
public class Table<T> {
public string GetSql_CreateTable() {
var sql = new StringBuilder();
foreach (var prop in typeof(T).GetProperties()) {
// generate create table sql
}
return sql.ToString();
}

public string Fill(List<T> rows) {
var sql = new StringBuilder();
// generate insert sql
return sql.ToString();
}
}
Where I'm stuck is how to specify one or more properties of the generic object to be part of a primary key. I'm not sure syntatically how to say, "use these properties for primary key". Any help would be appreciated!
5 Replies
phaseshift
phaseshift12mo ago
Probably you could use an expression, then parsing the expression for the property name. So for example with one T youd have string Fill<T, U>(Expression<Func<T, U>> primaryProperty) used like Fill<Foo>(foo => foo.Bar) EF always picks prim key by looking for a property name 'Id' - you could do the same
Camster
Camster12mo ago
Nice, i had a feeling there was a way to do it with expression trees. I'm not very knowledgeable on them, so need to do some more research. Also, in my case, I could have composite keys. Thank you, that helps a lot!
Accord
Accord12mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Camster
Camster12mo ago
!close
Accord
Accord12mo ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ What's the difference between `IConfiguration.Get` and `IConfiguration.Bind`?The docs say `Get` "Attempts to bind the configuration instance to a new instance of type T. If this❔ Best place to store a flag globally on ASP.NET CoreHello everyone. What I'm trying to achieve is given a list of users that each have access to 1...N ❔ C# app on LinuxHello , I'm trying to create a C# app on Linux but I think it's just not possible. I tried multiple ❔ Pattern matching on multiple cases with the same-typed membersHello, I am currently stuck with code like this: ```cs public void Foo(Bar bar) { if (bar is Bar1❔ Hosting CoreWCF service in AspNet Core (Kestrel). How do I validate the service is up?The output contains notifications that 1) the Kestrel server is listening on the port I specified an✅ What does "Windows Desktop" as the version in ASP.NET's documentation mean?https://learn.microsoft.com/en-us/dotnet/api/system.configuration?view=windowsdesktop-7.0❔ Number guessing game.I'm trying to make a program that generates a random number between 1-100, let's the user guess till❔ how would I got about making a discord bot.I am not new to coding but completely new to discord bot creation so need some pointers✅ Cannot figure out what I am doing wrong.I am currently taking classes for C#, and im kind of throwing things around with casts and things, i❔ Cannot find "New Project" for Hello WorldHey, I'm terribly sorry if this isn't in the right place. After all this time I'm still not used to