I'm creating a freelance task management app. Here is the logic. These are the 3 POCOs involved
User (LoggedIn User)
- A user can create any number of clients
- A user can create any number of projects
- A user is created, updated by another user
Client (Freelance Client)
- A client can have any number of projects
- A client is created, updated by a user
Project
- A project should have only one client
- A project is created, updated by a user
These are my Entities. I'm using Code-First approach and uses SQLServer. I removed many properties for posting here
User Entity
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
public virtual User CreatedBy { get; set; }
public virtual User UpdatedBy { get; set; }
[ForeignKey("UserId")]
public int? CreatedById { get; set; }
[ForeignKey("UserId")]
public int? UpdatedById { get; set; }
public virtual ICollection<Project> Projects { get; set; }
public virtual ICollection<Client> Clients { get; set; }
}
Client Entity
public class Client
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
public string Company { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public DateTime? CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
public virtual User CreatedBy { get; set; }
public virtual User UpdatedBy { get; set; }
[ForeignKey("UserId")]
public int? CreatedById { get; set; }
[ForeignKey("UserId")]
public int? UpdatedById { get; set; }
public ICollection<Project> Projects { get; set; }
}
Project Entity
public class Project
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string ProjectLogo { get; set; }
public DateTime? CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
public virtual User CreatedBy { get; set; }
public virtual User UpdatedBy { get; set; }
[ForeignKey("UserId")]
public int? CreatedById { get; set; }
[ForeignKey("UserId")]
public int? UpdatedById { get; set; }
public virtual Client Client { get; set; }
}
I'm now facing so many issues with ForginKeys and mappings. What is wrong with the entity mappings here?
I can't insert 2 rows in clients table with same CreatedById, EF is constraining it with unique values.
This is my fluent mappings
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//User
modelBuilder.Entity<User>().HasOne(a => a.CreatedBy).WithOne().OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<User>().HasOne(a => a.UpdatedBy).WithOne().OnDelete(DeleteBehavior.NoAction);
//Project
modelBuilder.Entity<Project>().HasOne(a => a.CreatedBy).WithOne().OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Project>().HasOne(a => a.UpdatedBy).WithOne().OnDelete(DeleteBehavior.NoAction);
//Client
modelBuilder.Entity<Client>().HasOne(a => a.CreatedBy).WithOne().OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Client>().HasOne(a => a.UpdatedBy).WithOne().OnDelete(DeleteBehavior.NoAction);
}
How can I implement this requirement?
question from:
https://stackoverflow.com/questions/65951571/ef-core-5-how-to-define-multiple-one-many-relationships 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…