C# with postgresql get 3221225477 (0xc0000005) 'Access violation

147 Views Asked by At

I am new to C# and trying to set up a C# API with Postgres. My database consists of two tables that have a 1:1 relation. Here are my tables in code:

    [Table("notification")]
    public class NotificationDb
    {
        [Key]
        public int NotificationId { get; set; }
        public string Sender { get; set; } = string.Empty;
        public string Recipient { get; set; } = string.Empty;
        public string Subject { get; set; } = string.Empty;
        public string Message { get; set; } = string.Empty;
        public string Platform { get; set; } = string.Empty;
        public string SendDate { get; set; } = string.Empty;
        public virtual SendStatusDb? SendStatus { get; set; } = new SendStatusDb();
    }

    [Table("sendStatus")]
    public class SendStatusDb
    {
        [Key]
        public int SendId { get; set; }
        public int Status { get; set; }
        public string StatusMessage { get; set; } = string.Empty;
        public int NotificationId { get; set; }
        public virtual NotificationDb Notification { get; set; } = new NotificationDb();
    }

In order for the relation to exist, I have defined it in the MailContext. It looks like this:

    public class MailContext: DbContext
    {
        public MailContext(DbContextOptions<MailContext> options): base(options) {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            
            modelBuilder.Entity<SendStatusDb>(entity =>
            {
                entity.HasOne(e => e.Notification)
                .WithOne(e => e.SendStatus)
                .HasForeignKey<SendStatusDb>(e => e.NotificationId);
            });
           

            //modelBuilder.UseSerialColumns();
        }

        public DbSet<NotificationDb> Notification { get; set; }

        public DbSet<SendStatusDb> SendStatus { get; set; }
    }

I created a migration and updated my database and everything worked so far. To test, I want to get all the data from the database by calling a GET URL in Swagger. The controller gets the call and uses the MailServiceCustom to get all the data from the database.

  [Route("[controller]")]
  [ApiController]
  public class MailController : ControllerBase
  {
      private readonly MailServiceCustom _mailService;

      public MailController(MailContext mailContext)
      {
          _mailService = new MailServiceCustom(mailContext);
      }
      [HttpGet]
      public IActionResult GetAllNotifications() {

          IEnumerable<NotificationModel> notifications = _mailService.GetAllNotifications();
          if (notifications.Any())
          {
              return Ok(notifications);
          }
          else
          {
              return NotFound();
          }
      }
}

    public class MailServiceCustom
    {
        private MailContext _mailContext;

        public MailServiceCustom(MailContext context)
        {
            _mailContext = context;
            
        }

        public List<NotificationModel> GetAllNotifications()
        {
            List<NotificationModel> response = new List<NotificationModel>();
            var data = _mailContext.Notification.Select(x => x);
            var test = data.ToList();
            test.ForEach(notificationData =>
            {

                response.Add(new NotificationModel
                {
                    NotificationId = notificationData.NotificationId,
                    Sender = notificationData.Sender,
                    Recipient = notificationData.Recipient,
                    SendDate = notificationData.SendDate,
                    Message = notificationData.Message,
                    Platform = notificationData.Platform,

                });

            });
            
            return response;
        }

But the database gives me an Access Violation error when I call the ToList() method,

If I remove the relation between the tables, then it works. But with this relation I always get an Access Violation.

1

There are 1 best solutions below

3
Razielruss On

I don't know why it works, but I have a solution. In SendStatus.cs I have to change

public virtual NotificationDb Notification { get; set; } = new NotificationDb();

in

public NotificationDb Notification { get; set; } = null!;

Then it works.