Data is not being written to SQL database.

huangapple go评论87阅读模式
英文:

Data is not being written to SQL database

问题

我正在尝试使用 ASP.NET Core 6 构建具有数据持久性的实时聊天应用程序。

我的问题是:我配置了 SignalR 并且它正常工作(在 UI 上进行更改)。然而,它不会更新数据库(我使用 Entity Framework Core 以代码优先的方式)。

我的应用程序 DbContext

  1. using Microsoft.EntityFrameworkCore;
  2. using VGChat_demo.Models;
  3. namespace VGChat_demo.Data
  4. {
  5. public class ApplicationDbContext : DbContext
  6. {
  7. public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
  8. {
  9. }
  10. public DbSet<Message> Messages { get; set; }
  11. }
  12. }

我的聊天 Hub:

  1. using Microsoft.AspNetCore.SignalR;
  2. using VGChat_demo.Data;
  3. using VGChat_demo.Models;
  4. namespace VGChat_demo.Hubs
  5. {
  6. public class ChatHub : Hub
  7. {
  8. private readonly ApplicationDbContext _dbContext;
  9. public ChatHub(ApplicationDbContext dbContext)
  10. {
  11. _dbContext = dbContext;
  12. }
  13. public async Task SendMessage(string user, string content)
  14. {
  15. var message = new Message
  16. {
  17. User = user,
  18. Content = content,
  19. Timestamp = DateTime.UtcNow
  20. };
  21. _dbContext.Messages.Add(message);
  22. await Clients.All.SendAsync("ReceiveMessage", message.User, message.Content, message.Timestamp);
  23. await _dbContext.SaveChangesAsync();
  24. }
  25. }
  26. }

我在以下位置设置了断点:

  1. await _dbContext.SaveChangesAsync();

程序运行到该行,但不会将任何内容保存到我的数据库(迁移成功,我得到了与我的消息模型同名的数据库)。

英文:

I am trying to build real time chat app with data persistence using ASP.NET Core 6.

My problem is: I configured SignalR and it works properly (it makes changes on UI). However, it does not update the database (I am using Entity Framework Core with a code-first approach).

My application DbContext:

  1. using Microsoft.EntityFrameworkCore;
  2. using VGChat_demo.Models;
  3. namespace VGChat_demo.Data
  4. {
  5. public class ApplicationDbContext : DbContext
  6. {
  7. public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
  8. {
  9. }
  10. public DbSet<Message> Messages { get; set; }
  11. }
  12. }

My chat hub:

  1. using Microsoft.AspNetCore.SignalR;
  2. using VGChat_demo.Data;
  3. using VGChat_demo.Models;
  4. namespace VGChat_demo.Hubs
  5. {
  6. public class ChatHub : Hub
  7. {
  8. private readonly ApplicationDbContext _dbContext;
  9. public ChatHub(ApplicationDbContext dbContext)
  10. {
  11. _dbContext = dbContext;
  12. }
  13. public async Task SendMessage(string user, string content)
  14. {
  15. var message = new Message
  16. {
  17. User = user,
  18. Content = content,
  19. Timestamp = DateTime.UtcNow
  20. };
  21. _dbContext.Messages.Add(message);
  22. await Clients.All.SendAsync("ReceiveMessage", message.User, message.Content, message.Timestamp);
  23. await _dbContext.SaveChangesAsync();
  24. }
  25. }
  26. }

I put a breakpoint on

  1. await _dbContext.SaveChangesAsync();

Program runs to that line, but it does not save anything to my database (migration is successful, I get the database named same with my message model)

答案1

得分: 1

我解决了。
显然是我在迁移后更改了字段名称。
迁移文件无法找到模型上对应的字段。

英文:

I solved it.
It's appearently I changed field names after migration.
Migration file could not find corresponding fields on model.

huangapple
  • 本文由 发表于 2023年5月20日 22:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定