I have two tables in my database, energyinfo (t1) and enerfyinfometers (t2).
I have a start and stop time in t1 like 500 min difference.
I want to add the time in between t1 (start and stop) for every one minute in t2.
Here's my code. it takes a lot of time to run and t1 entries is happening but fails to do t2.
if (dbContext == null)
{
dbContext = Context.Create("d:\\temp\\new.sdf", "");
var start = DateTime.Now;
DateTime time = DateTime.Now;
DateTime time2 = DateTime.Now;
// time = time + TimeSpan.FromMinutes(30);
time2 = time2 + TimeSpan.FromMinutes(1);
Random rnd = new Random();
double Counter = 50;
var stop = start.AddMinutes(15);
double value2 =36;
for (int i = 1; i < 36; i++)
{
Counter = Counter + 0.5;
double value3 = rnd.Next (2,12) +0.5;
double value4 = value3 / 2;
double value = rnd.Next(50) + 0.5;
value2 += (value / 60);
double roundedValue = Math.Ceiling(value2) + 1;
int LMH = rnd.Next(0, 3);
dbContext.EnergyInfo.Add(new EnergyInfo()
{ EId = i,
Timestamp = time, type = LMH,
startTime = start, stopTime = stop,
demandCharge = value3,
threshHold = 70,
normalCharge = value4,
peakDuration = 900 });
dbContext.SaveChanges();
for (var x = start; x < stop; x.AddMinutes(1))
{
var ob = new EnergyMeterInfo() { Timestamp = x, MeterId = 5,
powerConsumptionKW = Counter,
cumlativePwrConsumption = roundedValue,
EnergyInfoId = i };
using (dbContext.Database.BeginTransaction())
{
dbContext.EnergyMeterInfo.Add(ob);
}
}
start = stop;
stop = start.AddMinutes(500);
dbContext.SaveChanges();
}
}
MessageBox.Show(dbContext.EnergyInfo.Count() + " Records found !" + dbContext.EnergyMeterInfo.Count() + " found");
x.AddMinutes(1). This is effectively adding 1 minute every time the loop has completed, thereforexwill never be greater thanstop. Looks like you need to rethink your for loop logic.