C
C#6mo ago
ZerkyXII

Multiple 1 to Many Relationships in Entity Framework

Hello, I just am looking on getting some documentation or something on entity framework core. I have a main Project Table, this Project Table has many tables connected like Tasks, Users, Clients, Phases, etc. All of these other tables are 1 to Many so my projects has a lot of 1 to many relationship(1 project has many tasks, and a task only has 1 project). I'm struggling to figure out how to do this. Any documentation or resources would be awesome!
17 Replies
mg
mg6mo ago
So a project has many tasks, and many users, and many clients, etc? It's the same way you do a single one to many relationship, you just do it multiple times
ZerkyXII
ZerkyXIIOP6mo ago
Yeah I tried that but it just isn't happy. I'll get you an error code one sec
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'TasksID', table 'rnpm.dbo.Tasks'; column does not allow nulls. INSERT fails.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'TasksID', table 'rnpm.dbo.Tasks'; column does not allow nulls. INSERT fails.
AppDBContext :
public class AppDBContext : DbContext
{
public AppDBContext(DbContextOptions options) : base(options)
{

}
public DbSet<Projects> Projects { get; set; }
public DbSet<Phases> Phases { get; set; }
public DbSet<Tasks> Tasks { get; set; }
public class AppDBContext : DbContext
{
public AppDBContext(DbContextOptions options) : base(options)
{

}
public DbSet<Projects> Projects { get; set; }
public DbSet<Phases> Phases { get; set; }
public DbSet<Tasks> Tasks { get; set; }
So I have Phases, that is working all good. So I pretty much copied the exact thing for Tasks. But even with adding onModelBuilder it's still pissed hahaha
Saber
Saber6mo ago
One-to-many relationships - EF Core
How to configure one-to-many relationships between entity types when using Entity Framework Core
ZerkyXII
ZerkyXIIOP6mo ago
[Table("Projects")]
public class Projects
{
// Project SQL Table Properties
public int ProjectsID { get; set; }
public string ProjectsName { get; set; } = String.Empty;
public string ProjectsShortcode { get; set; } = String.Empty;
public string ProjectsDescription { get; set; } = String.Empty;
// Phases Link
public List<Phases> Phases { get; set; } = new List<Phases>();
// Tasks Link
public List<Tasks> Tasks { get; set; } = new List<Tasks>();
}
[Table("Projects")]
public class Projects
{
// Project SQL Table Properties
public int ProjectsID { get; set; }
public string ProjectsName { get; set; } = String.Empty;
public string ProjectsShortcode { get; set; } = String.Empty;
public string ProjectsDescription { get; set; } = String.Empty;
// Phases Link
public List<Phases> Phases { get; set; } = new List<Phases>();
// Tasks Link
public List<Tasks> Tasks { get; set; } = new List<Tasks>();
}
[Table("Phases")]
public class Phases
{
public int PhasesID { get; set; }
public string PhasesName { get; set; } = String.Empty;
public string PhasesDescription { get; set; } = String.Empty;
public string PhasesStartDate { get; set; } = String.Empty;
public string PhasesEndDate { get; set; } = String.Empty;
// Project Link
public int? ProjectsID { get; set; }
public Projects? Projects { get; set; }
}
[Table("Phases")]
public class Phases
{
public int PhasesID { get; set; }
public string PhasesName { get; set; } = String.Empty;
public string PhasesDescription { get; set; } = String.Empty;
public string PhasesStartDate { get; set; } = String.Empty;
public string PhasesEndDate { get; set; } = String.Empty;
// Project Link
public int? ProjectsID { get; set; }
public Projects? Projects { get; set; }
}
[Table("Tasks")]
public class Tasks
{
public int TasksID { get; set; }
public string TasksName { get; set; } = String.Empty;
public string TasksDescription { get; set; } = String.Empty;
public string TasksStatus { get; set; } = String.Empty;
public string TasksStartDate { get; set; } = String.Empty;
public string TasksEndDate { get; set; } = String.Empty;

public int? ProjectsID { get; set; }
public Projects? Projects { get; set; }
}
[Table("Tasks")]
public class Tasks
{
public int TasksID { get; set; }
public string TasksName { get; set; } = String.Empty;
public string TasksDescription { get; set; } = String.Empty;
public string TasksStatus { get; set; } = String.Empty;
public string TasksStartDate { get; set; } = String.Empty;
public string TasksEndDate { get; set; } = String.Empty;

public int? ProjectsID { get; set; }
public Projects? Projects { get; set; }
}
mg
mg6mo ago
The error indicates you're trying to insert null into a non-nullable column namely TasksID
ZerkyXII
ZerkyXIIOP6mo ago
Right that's where I'm confused cause the code is the same as phases(just replaced as tasks)
mg
mg6mo ago
It looks like it's the primary key of the Tasks table, which should indeed not be nullable What insert statement is causing the error?
ZerkyXII
ZerkyXIIOP6mo ago
tasks it like won't auto increment the ID like the phases
mg
mg6mo ago
Also, you should name your classes in the singular form e.g. Project instead of Projects
ZerkyXII
ZerkyXIIOP6mo ago
Ik I got too deep into it and I'm gonna have to go back and rebuild it so I'm just trying to get the linking down But you're def right haha Ik it's super confusing but maybe should I just share a git link cause I got repository/interfaces/dto/mappers/controllers that all connect. I'm thinking i'm beefing something up
mg
mg6mo ago
To start all we need is the stack trace of that exception to see what caused it It's really weird that it's being thrown, because the property is a non nullable value type, so EF shouldn't be sending a query that tries to insert null into that column
ZerkyXII
ZerkyXIIOP6mo ago
that's the full error
mg
mg6mo ago
so what's happening at TasksRepository.cs:line 28 It's also worth noting that your dbcontext is itself a repository, and you don't need to layer another one on top of it
ZerkyXII
ZerkyXIIOP6mo ago
public async Task<Tasks> CreateAsync(Tasks tasksModel)
{
await _context.Tasks.AddAsync(tasksModel);
await _context.SaveChangesAsync();
return tasksModel;
}
public async Task<Tasks> CreateAsync(Tasks tasksModel)
{
await _context.Tasks.AddAsync(tasksModel);
await _context.SaveChangesAsync();
return tasksModel;
}
mg
mg6mo ago
Put a breakpoint in and examine the properties of tasksModel See what the value of TasksID is Though it's impossible for it to be null, so I'm honestly not sure what's going on Also don't use AddAsync()
ZerkyXII
ZerkyXIIOP6mo ago
public async Task<Phases> CreateAsync(Phases phasesModel)
{
await _context.Phases.AddAsync(phasesModel);
await _context.SaveChangesAsync();
return phasesModel;
}
public async Task<Phases> CreateAsync(Phases phasesModel)
{
await _context.Phases.AddAsync(phasesModel);
await _context.SaveChangesAsync();
return phasesModel;
}
so that from the Phases works totally fine. It's not until I add the tasks to the repo. Trying out the breakpoint to see if it can give me the deats though So it just works now, idk I didn't do anything. Redid the migration a few times after testing a few things and it just works..
Want results from more Discord servers?
Add your server