❔ Generating SQL strings in a method?

Hello there, I want to create a method that can generate me a SQL string based upon the parameters passes I've thought of having a Table Name, Table Obj (obj data is update by entry form), Key Field name and key value that would be something like
public static ExtSQL(this DbContext _context, string Table_Name, dynamic TableObj, string KeyFileld, string KeyValue) this method will check if given value exist in Table or not (this part is done) but when it comes to generating insert SQL or update i cant figure out how or what ways should take to achieve this.
9 Replies
Angius
Angius2y ago
So you want to create your own ORM for... some reason
ohthatstheone
ohthatstheoneOP2y ago
kind of
Angius
Angius2y ago
Why not just use an ORM, then? Entity Framework is right there
ohthatstheone
ohthatstheoneOP2y ago
it is but it lacks add or update and also that doesn't fit with want it want to achieve in the project i'm poring on
Angius
Angius2y ago
What do you mean it lacks add or update..?
_context.Things.Add(new Thing());
await _context.SaveChangesAsync();
_context.Things.Add(new Thing());
await _context.SaveChangesAsync();
await _context.Things
.Where(t => t.Id == id)
.ExecuteUpdateAsync(t => t.SetProp(x => x.Name = name));
await _context.Things
.Where(t => t.Id == id)
.ExecuteUpdateAsync(t => t.SetProp(x => x.Name = name));
ohthatstheone
ohthatstheoneOP2y ago
in this case i have to set each prop... i have 50 to 150 props in each table
Angius
Angius2y ago
There are other ways of performing updates
var thing = await _context.Things.FindAsync(id);
thing.Name = name;
await _context.SaveChangesAsync();
var thing = await _context.Things.FindAsync(id);
thing.Name = name;
await _context.SaveChangesAsync();
or
var thing = new Thing();
thing.Id = id;
_context.Attach(thing);
_context.Update(thing);
await _context.SaveChangesAsync();
var thing = new Thing();
thing.Id = id;
_context.Attach(thing);
_context.Update(thing);
await _context.SaveChangesAsync();
Or, sure, you can go with your idea It'll be a lot of string building and type-unsafe code, though
ohthatstheone
ohthatstheoneOP2y ago
mostly i my data in in string or decimal that too have no way of putting special chars... that are already checked in front end
Accord
Accord2y ago
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