My page model calls a method that executes a query.
// Retrieve financials
var details = await (from detail in dbContext.StorageDetails
where (detail.ArrivalDate != null && detail.ArrivalDate <= dateRange.EndDate &&
(detail.DepartureDate == null || detail.DepartureDate >= dateRange.StartDate)) ||
(detail.TakeOrPayStartDate != null && detail.TakeOrPayStartDate <= dateRange.EndDate &&
(detail.TakeOrPayEndDate == null || detail.TakeOrPayEndDate >= dateRange.StartDate))
orderby detail.Location.City, detail.Location.State, detail.ArrivalDate
select new
{
Location = new
{
Id = detail.LocationId,
detail.Location.Name,
detail.Location.City,
detail.Location.State
},
detail.RailcarNumber,
detail.TakeOrPayId,
detail.TakeOrPayStartDate,
detail.TakeOrPayEndDate,
detail.ArrivalDate,
detail.DepartureDate,
detail.CherryPickDate,
detail.DailyStorageRate,
detail.SwitchInRate,
detail.SwitchOutRate,
detail.CherryPickRate,
detail.DailyStorageCost,
detail.SwitchInCost,
detail.SwitchOutCost,
detail.CherryPickCost,
})
.ToListAsync();
I set a breakpoint at the start of this query. When I step over it my code stops in my page's CSHTML file, complaining that the variable that would've held the result of this query is null
!
If instead of stepping over this query, I step into it, I find myself stepping through ApplicationDbContact.OnModelCreating()
.
I thought OnModelCreating()
was just used for generating the database schema. So this doesn't make any sense at all to me.
Is there anyone that can see something here, or have I completely broke ASP.NET Core? How would anyone begin to troubleshoot something like this?
This is my page model handler. The query above is in helper.GetFinancialsByLocation()
.
public async void OnGetAsync()
{
DateTime date = DateTime.Today;
DateRange = new DateRange
{
StartDate = new DateTime(date.Year, date.Month, 1),
};
DateRange.EndDate = DateRange.StartDate.AddMonths(1).AddMilliseconds(-1);
try
{
FinancialsHelper helper = new FinancialsHelper();
FinancialsLocations = await helper.GetFinancialsByLocation(DbContext, DateRange);
}
catch (Exception ex)
{
string s = ex.Message;
}
}
Here's my startup.cs.
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
As per the docs you should be using:
public async Task OnGetAsync()
rather than:
public async void OnGetAsync()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.