i'm having trouble getting repository insert database.
my context:
public class context : dbcontext { public context() : base("mydatabase") { } public dbset<appartment> appartments { get; set; } public dbset<sensor> sensors { get; set; } public dbset<measurement> measurements { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<measurement>().haskey(m => new {m.appartmentid, m.sensorid}); modelbuilder.entity<measurement>() .hasrequired(m => m.appartment); modelbuilder.entity<measurement>() .hasrequired(m => m.sensor); } }
my entity:
public class appartment { public int appartmentid { get; set; } public int floor { get; set; } public int number { get; set; } public double size { get; set; } public icollection<measurement> measurements { get; set; } public appartment() { } public appartment(int floor, int number, int size) { floor = floor; number = number; size = size; } }
my repository:
public class repository<t> t : class { private context _context; public repository(context context) { _context = context; } public async task<int> add(t t) { _context.set<t>().add(t); return await _context.savechangesasync(); }
my test:
class program { static void main(string[] args) { var context = new context(); var apprepos = new repository<appartment>(context); var test = new appartment(1,1,1); apprepos.add(test); }
using this, appartment object isn't inserted database.
am missing completely?
thanks in advance.
you need await apprepos.add since async method. otherwise, program terminate before async method executed.
await apprepos.add(test);