C
C#10mo ago
Whiteboy

✅ MySQL with Entity Framework, getting null of entity all time

public class UserAssignmentDate
{
[ForeignKey("UserId")]
public int UserId { get; set; }

[ForeignKey("AssignmentId")]
public int AssignmentId { get; set; }

public DateTimeOffset AssignmentTime { get; set; }
public DateTimeOffset DeadLineDateTime { get; set; }
public DateTimeOffset LastUploadDateTime { get; set; }
public TimeSpan TimeToNextUpload { get; set; }

// Navigation properties
public User User { get; set; }
public Assignment Assignment { get; set; }
}
public class UserAssignmentDate
{
[ForeignKey("UserId")]
public int UserId { get; set; }

[ForeignKey("AssignmentId")]
public int AssignmentId { get; set; }

public DateTimeOffset AssignmentTime { get; set; }
public DateTimeOffset DeadLineDateTime { get; set; }
public DateTimeOffset LastUploadDateTime { get; set; }
public TimeSpan TimeToNextUpload { get; set; }

// Navigation properties
public User User { get; set; }
public Assignment Assignment { get; set; }
}
var userAssignmentDate = await dbContext.UserAssignmentDates
.FirstOrDefaultAsync(userAssignmentDate => userAssignmentDate.UserId == UserId && userAssignmentDate.AssignmentId == assignmentId);
var userAssignmentDate = await dbContext.UserAssignmentDates
.FirstOrDefaultAsync(userAssignmentDate => userAssignmentDate.UserId == UserId && userAssignmentDate.AssignmentId == assignmentId);
foreach (int userId in userIds)
{
var userAssignmentDate = new UserAssignmentDate
{
UserId = userId,
AssignmentId = assignmentId,
AssignmentTime = assignmentTime,
LastUploadDateTime = DateTimeOffset.MinValue,
};

dbContext.UserAssignmentDates.Add(userAssignmentDate);
}

await dbContext.SaveChangesAsync();
foreach (int userId in userIds)
{
var userAssignmentDate = new UserAssignmentDate
{
UserId = userId,
AssignmentId = assignmentId,
AssignmentTime = assignmentTime,
LastUploadDateTime = DateTimeOffset.MinValue,
};

dbContext.UserAssignmentDates.Add(userAssignmentDate);
}

await dbContext.SaveChangesAsync();
and userAssignmentDate returns null all time when UserId and AssignmentId are good values
13 Replies
Angius
Angius10mo ago
You need to explicitly load the related data with .Include() Or better, .Select()
Whiteboy
WhiteboyOP10mo ago
var userAssignmentDate = await dbContext.UserAssignmentDates
.Include(ua => ua.User)
.Include(ua => ua.Assignment)
.Where(assignmentDate => assignmentDate.UserId == UserId && assignmentDate.AssignmentId == assignmentId)
.FirstOrDefaultAsync();

if (userAssignmentDate != null)
{
userAssignmentDate.LastUploadDateTime = DateTimeOffset.Now;
await dbContext.SaveChangesAsync();
}

var userAssignmentDate = await dbContext.UserAssignmentDates
.Include(ua => ua.User)
.Include(ua => ua.Assignment)
.Where(assignmentDate => assignmentDate.UserId == UserId && assignmentDate.AssignmentId == assignmentId)
.FirstOrDefaultAsync();

if (userAssignmentDate != null)
{
userAssignmentDate.LastUploadDateTime = DateTimeOffset.Now;
await dbContext.SaveChangesAsync();
}

so now it's not null but the time is not updating D:
private void GetUserTaskRemainingTime(int assignmentId)
{
var userAssignmentDate = dbContext.UserAssignmentDates.FirstOrDefault(userAssignmentDate => userAssignmentDate.AssignmentId == assignmentId && userAssignmentDate.UserId == UserId);
if (userAssignmentDate == null || Assignment == null) return;

RemainingTimeToDoAssignment = userAssignmentDate.DeadLineDateTime - DateTimeOffset.Now;
if (userAssignmentDate.LastUploadDateTime == DateTimeOffset.MinValue)
{
TimeToNextUpload = TimeSpan.Zero;
}
else
{
TimeToNextUpload = userAssignmentDate.LastUploadDateTime + Assignment.UploadFrequency - DateTimeOffset.Now;
}
}
private void GetUserTaskRemainingTime(int assignmentId)
{
var userAssignmentDate = dbContext.UserAssignmentDates.FirstOrDefault(userAssignmentDate => userAssignmentDate.AssignmentId == assignmentId && userAssignmentDate.UserId == UserId);
if (userAssignmentDate == null || Assignment == null) return;

RemainingTimeToDoAssignment = userAssignmentDate.DeadLineDateTime - DateTimeOffset.Now;
if (userAssignmentDate.LastUploadDateTime == DateTimeOffset.MinValue)
{
TimeToNextUpload = TimeSpan.Zero;
}
else
{
TimeToNextUpload = userAssignmentDate.LastUploadDateTime + Assignment.UploadFrequency - DateTimeOffset.Now;
}
}
cs
var userAssignmentDate = await dbContext.UserAssignmentDates
.Include(ua => ua.User)
.Include(ua => ua.Assignment)
.Where(assignmentDate => assignmentDate.UserId == UserId && assignmentDate.AssignmentId == assignmentId)
.FirstOrDefaultAsync();

if (userAssignmentDate != null)
{
userAssignmentDate.LastUploadDateTime = DateTimeOffset.Now;
await dbContext.SaveChangesAsync();
}

await dbContext.SaveChangesAsync();

PageHelper.SetTempDataSuccessMessage($"last up time: {userAssignmentDate.LastUploadDateTime}.", TempData);
return await Task.FromResult<IActionResult>(RedirectToPage("/AssignmentView", new
{
assignment.AssignmentId,
}));
cs
var userAssignmentDate = await dbContext.UserAssignmentDates
.Include(ua => ua.User)
.Include(ua => ua.Assignment)
.Where(assignmentDate => assignmentDate.UserId == UserId && assignmentDate.AssignmentId == assignmentId)
.FirstOrDefaultAsync();

if (userAssignmentDate != null)
{
userAssignmentDate.LastUploadDateTime = DateTimeOffset.Now;
await dbContext.SaveChangesAsync();
}

await dbContext.SaveChangesAsync();

PageHelper.SetTempDataSuccessMessage($"last up time: {userAssignmentDate.LastUploadDateTime}.", TempData);
return await Task.FromResult<IActionResult>(RedirectToPage("/AssignmentView", new
{
assignment.AssignmentId,
}));
How is it null when i wanna chcek value?? @ZZZZZZZZZZZZZZZZZZZZZZZZZ
Angius
Angius10mo ago
Where is it null, exactly?
Whiteboy
WhiteboyOP10mo ago
PageHelper.SetTempDataSuccessMessage($"last up time: {userAssignmentDate.LastUploadDateTime}.", TempData);
PageHelper.SetTempDataSuccessMessage($"last up time: {userAssignmentDate.LastUploadDateTime}.", TempData);
here
Angius
Angius10mo ago
And the whole assignment is null, or just the upload date?
Whiteboy
WhiteboyOP10mo ago
No description
Whiteboy
WhiteboyOP10mo ago
No description
Whiteboy
WhiteboyOP10mo ago
.
Angius
Angius10mo ago
Well, apparently there's no such assignment Try commenting out the Where() clause, see if that gives results
Whiteboy
WhiteboyOP10mo ago
No description
Whiteboy
WhiteboyOP10mo ago
idk there seen to be 6 results but for some reason i can't get to them
Angius
Angius10mo ago
Make sure that UserId and assignmentId are both 1, for example Since I can see you have an entry like that Issue seems to be with what you're trying to query by
Whiteboy
WhiteboyOP10mo ago
im gonna lose my mind getting this project to very end ... the issue was with passing userId = 0 which obviously doesn't exist So i just get correct userId and it works just fine
Want results from more Discord servers?
Add your server