'How can I have 2 foreign keys pointing to the same table using fluent api
I need help with my project: I want to have a section for the comments, and in each comment I want to have the text of the comment, the username and the user image, so I need to have an user id for the comment, but the user already has a connection with pages (every user has his page like patreon).
If you need here it is my github repository: https://github.com/diogosousa034/xPatreon
This is the OnMdelCreating
method that I have:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasKey(o => o.User_ID);
modelBuilder.Entity<User>()
.Property(e => e.User_ID)
.ValueGeneratedOnAdd();
modelBuilder.Entity<User>()
.HasIndex(u => u.UserName)
.IsUnique();
modelBuilder.Entity<Page>()
.HasKey(o => o.Page_ID);
modelBuilder.Entity<Page>()
.Property(e => e.Page_ID)
.ValueGeneratedOnAdd();
modelBuilder.Entity<Page>()
.HasIndex(u => u.PageName)
.IsUnique();
modelBuilder.Entity<PageContent>()
.HasKey(o => o.Content_ID);
modelBuilder.Entity<PageContent>()
.Property(e => e.Content_ID)
.ValueGeneratedOnAdd();
modelBuilder.Entity<Patrons>()
.HasKey(o => o.PatronFollow_ID);
modelBuilder.Entity<Patrons>()
.Property(e => e.PatronFollow_ID)
.ValueGeneratedOnAdd();
modelBuilder.Entity<ContentComments>()
.HasKey(o => o.Comment_ID);
modelBuilder.Entity<ContentComments>()
.Property(e => e.Comment_ID)
.ValueGeneratedOnAdd();
modelBuilder.Entity<Page>()
.HasOne<User>(s => s.User)
.WithOne(g => g.Page)
.HasForeignKey<Page>(s => s.User_ID)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<PageContent>()
.HasOne<Page>(s => s.Page)
.WithMany(g => g.Contents)
.HasForeignKey(s => s.Page_ID)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Patrons>()
.HasOne<Page>(s => s.Page)
.WithMany(g => g.patrons)
.HasForeignKey(s => s.Page_ID)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ContentComments>()
.HasOne<PageContent>(s => s.PageContent)
.WithMany(g => g.Comments)
.HasForeignKey(s => s.Content_ID)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<ContentComments>()
.HasOne<User>(s => s.user)
.WithMany(g => g.comments)
.HasForeignKey(s => s.User_ID)
.OnDelete(DeleteBehavior.Restrict);
}
And this is my User
model class:
public class User
{
public int User_ID { get; set; }
[Column(TypeName = "nvarchar(100)")]
//[Required(ErrorMessage = "Username is required.")]
public string UserName { get; set; }
[Column(TypeName = "nvarchar(100)")]
//[Required(ErrorMessage = "Email is required.")]
public string Email { get; set; }
[Column(TypeName = "nvarchar(200)")]
public string Image { get; set; }
[Column(TypeName = "nvarchar(100)")]
//[Required(ErrorMessage = "Password is required.")]
//[DataType(DataType.Password)]
public string Password { get; set; }
[Column(TypeName = "nvarchar(100)")]
//[Required(ErrorMessage = "ConfirmPassword is required.")]
//[DataType(DataType.Password)]
//[Compare("Password", ErrorMessage = "Password and confirm password must be equals.")]
public string ConfirmPassword { get; set; }
[Column(TypeName = "nvarchar(30)")]
//[Required(ErrorMessage = "Role is required.")]
public string Role { get; set; }
[Column(TypeName = "datetime2")]
public DateTime RegistrationData { get; set; }
[NotMapped]
public IFormFile FrontImage { get; set; }
public virtual IEnumerable<ContentComments> comments { get; set; }
public virtual Page Page { get; set; }
}
This is my Comment
model class:
public class ContentComments
{
public int Comment_ID { get; set; }
[Column(TypeName = "nvarchar(MAX)")]
public String CommentText { get; set; }
public DateTime CommentData { get; set; }
public int User_ID { get; set; }
public virtual User user { get; set; }
public int Content_ID { get; set; }
public virtual PageContent PageContent { get; set; }
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|