当我将应用部署到Azure并将时间跨度保存到数据库时,我认为它是UTC

rpppsulh  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(120)

当我将我的Blazor应用部署到Azure并将TimeSpan保存到我的数据库时,我认为它是在UTC中?在Azure中保存DateTime内容的典型模式是什么?这是一个小型的应用程序,是否可以在我的本地时间保存到数据库,如果可以,如何保存?

  1. <InputText type="time" class="form-control form-control-sm" @bind-Value="Model.TimeIn" />
  2. <InputText type="time" class="form-control form-control-sm" @bind-Value="Model.TimeOut" />

字符串

s3fp2yjn

s3fp2yjn1#

这段代码可以让我将本地时间添加到SQL数据库中。我已经创建了一个默认的Blazor应用程序示例,并添加了一个Add Time页面。

#我的目录

  1. BlazorApp1/
  2. ├── models/
  3. ├── Model.cs
  4. |
  5. ├── Data/
  6. ├── Adddbcontext.cs
  7. ├── ...
  8. ├── Pages/
  9. ├── Index.razor
  10. ├── Counter.razor
  11. ├── FetchData.razor
  12. └── AddTime.razor
  13. | ├── ...
  14. ├── Shared/
  15. ├── MainLayout.razor
  16. ├── NavMenu.razor
  17. └── ...
  18. ├── App.razor
  19. ├── Program.cs
  20. └── ...

字符串

adddbcontext.cs

  1. using BlazorApp1.models;
  2. using Microsoft.EntityFrameworkCore;
  3. namespace BlazorApp1.Data
  4. {
  5. public class Adddbcontext : DbContext
  6. {
  7. private readonly IConfiguration Config;
  8. public Adddbcontext(IConfiguration configuration)
  9. {
  10. Config = configuration;
  11. }
  12. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  13. {
  14. optionsBuilder.UseSqlServer(Config.GetConnectionString("Dbconnectionstring"));
  15. }
  16. protected override void OnModelCreating(ModelBuilder modelBuilder)
  17. {
  18. modelBuilder.Entity<Model>().HasKey(e => e.Id);
  19. }
  20. public DbSet<Model> Time { get; set; }
  21. }
  22. }

Model.cs

  1. namespace BlazorApp1.models
  2. {
  3. public class Model
  4. {
  5. public int Id { get; set; }
  6. public string Timein { get; set; }
  7. public string TimeOut { get; set; }
  8. }
  9. }

AddTime.razor

  1. @page "/addtime"
  2. @using BlazorApp1.models;
  3. @inject Data.Adddbcontext Dbcontext;
  4. <PageTitle>AddTime</PageTitle>
  5. <h3>savetime</h3>
  6. <EditForm Model="@newtime" OnValidSubmit="test">
  7. <InputText type="time" class="form-control form-control-sm" @bind-Value="newtime.Timein"/>
  8. <InputText type="time" class="form-control form-control-sm" @bind-Value="newtime.TimeOut" />
  9. <button type="submit">Save</button>
  10. @if(save)
  11. {
  12. <p>Time is saved</p>
  13. }
  14. </EditForm>
  15. @code {
  16. public Model newtime = new Model();
  17. public bool save = false;
  18. private async Task test()
  19. {
  20. try
  21. {
  22. Dbcontext.Time.Add(newtime);
  23. await Dbcontext.SaveChangesAsync();
  24. save = true;
  25. }
  26. catch
  27. {
  28. throw;
  29. }
  30. }
  31. }


在脚本中将此添加到NavMenu.razor现有代码中以获取Add Time菜单。

  1. <div class="nav-item px-3">
  2. <NavLink class="nav-link" href="addtime">
  3. <span class="oi oi-list-rich" aria-hidden="true"></span> Add Time
  4. </NavLink>
  5. </div>

OUTPUT

本地时间以.

的形式显示


时间在SQL数据库中保存为24小时格式


展开查看全部

相关问题