I am trying to learn asp.net core and I am trying to implement a reddit like website.
what I am trying to do is when a subreddit is called, I want to load both admins and the members of the that particular subreddit and pass them to viewmodel.
to achieve this i have made two tables
public class MemberSubReddit
{
public long SubRedditId { get; set; }
public virtual SubReddit SubReddit { get; set; }
public string UserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
// the names differ since I was trying to check if the problem is from naming conventions
public class AdminSubReddit
{
public long SubRedditId { get; set; }
public virtual SubReddit SubReddit { get; set; }
public string AspNetUsersId { get; set; }
public virtual ApplicationUser AspNetUsers { get; set; }
}
now when I execute this code below:
var sub = _subRedditRepository.GetSingle(e => e.Name == name, s => s.Posts, s => s.Admins, s => s.Members);
the SubRedditId and SubReddit object loads in both admins and members, but as for User object only the UserId(AspNetUserId) loads and the ApplicationUser do not load.
also this is my modelbuilder code (based on asp.net core website in entityframework core, many to many connections are not handled automatically and you have to do it manually):
modelBuilder.Entity<AdminSubReddit>()
.HasKey(t => new { t.SubRedditId, t.AspNetUsersId });
modelBuilder.Entity<MemberSubReddit>()
.HasKey(t => new { t.SubRedditId, t.UserId });
modelBuilder.Entity<MemberSubReddit>()
.HasOne(us => us.ApplicationUser)
.WithMany(ms => ms.MemberIn)
.HasForeignKey(us => us.UserId);
modelBuilder.Entity<MemberSubReddit>()
.HasOne(ms => ms.SubReddit)
.WithMany(ms => ms.Members)
.HasForeignKey(ms => ms.SubRedditId);
modelBuilder.Entity<AdminSubReddit>()
.HasOne(us => us.AspNetUsers)
.WithMany(ms => ms.AdminIn)
.HasForeignKey(us => us.AspNetUsersId);
modelBuilder.Entity<AdminSubReddit>()
.HasOne(ms => ms.SubReddit)
.WithMany(ms => ms.Admins)
.HasForeignKey(ms => ms.SubRedditId);
so please tell me what I am doing wrong :D
also since I am new to this if you can see any structural problem with the code I would be happy if you point them out.
EDIT
public T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> query = _context.Set<T>();
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
return query.Where(predicate).FirstOrDefault();
}
This is the method which is getting called in the controller
Related
I'm trying to figure out this why this entity code is not working. When I call the following code,
await _context.ApplicationUsers
.Include(u => u.SentFriendRequests)
.Include(u => u.ReceievedFriendRequests)
.Include(u => u.Friends)
.FirstOrDefaultAsync()
I get a message
Lambda expression used inside Include is not valid
when I include Friends. Another weird issue if I don't include the friends property, it finds the list of friends, but each item only has RequestedBy object and all RequestedTo users are null. It only finds the RequestedToId.
Here's the code:
public class ApplicationUser : IdentityUser
public ApplicationUser()
{
SentFriendRequests = new List<Friend>();
ReceievedFriendRequests = new List<Friend>();
}
public string Name { get; set; }
public ICollection<Friend> SentFriendRequests { get; set; }
public ICollection<Friend> ReceievedFriendRequests { get; set; }
[NotMapped]
public ICollection<Friend> Friends
{
get
{
var friends = SentFriendRequests.Where(x => x.Approved).ToList();
friends.AddRange(ReceievedFriendRequests.Where(x => x.Approved));
return friends;
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<FriendRequest>()
.HasKey(t => new { t.RequestedById, t.RequestedToId });
modelBuilder.Entity<Friend>()
.HasOne(a => a.RequestedBy)
.WithMany(b => b.SentFriendRequests)
.HasForeignKey(c => c.RequestedById);
modelBuilder.Entity<Friend>()
.HasOne(a => a.RequestedTo)
.WithMany(b => b.ReceievedFriendRequests)
.HasForeignKey(c => c.RequestedToId);
}
public class Friend
{
public int RequestedById { get; set; }
public int RequestedToId { get; set; }
public ApplicationUser RequestedBy { get; set; }
public ApplicationUser RequestedTo { get; set; }
public FriendRequestFlag FriendRequestFlag { get; set; }
[NotMapped]
public bool Approved => FriendRequestFlag == FriendRequestFlag.Approved;
}
public enum FriendRequestFlag
{
New,
Approved,
Rejected,
Blocked,
Spam
};
So the first issue is that your Friends collection is NotMapped, therefore, you can't add it to an Include Statement since EFCore doesn't know/care about the property (Which is what you wanted by adding NotMapped).
Your second problem is that when you do an "Include", it's only one level deep. If you have navigational properties on that object, you need to keep going. For example :
await _context.ApplicationUsers
.Include(u => u.SentFriendRequests)
.ThenInclude(u => u.RequestedTo)//This is in the context of SendFriendRequests
.Include(u => u.ReceievedFriendRequests)
.ThenInclude(u => u.RequestedTo)//This is in the context of ReceivedFriendRequests
.FirstOrDefaultAsync()
The problem you are going to find is that, you will have to pre-define how deep you want your connections to go (Since you have what looks to be a recursive type model).
It's probably better to instead create a DTO and select out exactly what you need so then you don't do an overselect.
I am having difficulty getting EF7 to populate the objects referenced in a many to many join. I have followed the docs at https://docs.efproject.net/en/latest/modeling/relationships.html, but the object is still null. From what I can tell you don't have to do anything specific to get EF to populate them. I copied the sample code from the docs page as follows:
public class MyContext : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PostTag>()
.HasKey(t => new { t.PostId, t.TagId });
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Post)
.WithMany(p => p.PostTags)
.HasForeignKey(pt => pt.PostId);
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Tag)
.WithMany(t => t.PostTags)
.HasForeignKey(pt => pt.TagId);
}
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public List<PostTag> PostTags { get; set; }
}
public class Tag
{
public string TagId { get; set; }
public string Title { get; set; }
public List<PostTag> PostTags { get; set; }
}
public class PostTag
{
public int PostId { get; set; }
public Post Post { get; set; }
public string TagId { get; set; }
public Tag Tag { get; set; }
}
I had to add a constructor to get it to run. I also added a Title field to the Tag class so it has more than just a key. Once I populated some data in the tables, I run the following code to retrieve from the database:
var results = _context.Posts.Include(s => s.PostTags).ToList();
When I examine the results in the debugger, the Tag objects are null even though the keys to obtain them are present. Notice that the Post objects are populated. It is always the second column of the two column key that is not joined:
This is the SQL generated by EF7:
SELECT [s].[PostId], [s].[Content], [s].[Title]
FROM [Posts] AS [s]
ORDER BY [s].[PostId]
SELECT [p].[PostId], [p].[TagId]
FROM [PostTag] AS [p]
WHERE EXISTS (
SELECT 1
FROM [Posts] AS [s]
WHERE [p].[PostId] = [s].[PostId])
ORDER BY [p].[PostId]
It doesn't appear to be fetching the Tag object at all. What am I missing here?
For completeness, I have included the sample data:
Thanks to #SOfanatic for pointing me in the right direction. I am not sure why EF doesn't automatically load the second reference class, but it doesn't. The following code will retrieve the Tag object (as well as the Post object even though we don't explicitly load it).
var results = _context.Posts.Include(s => s.PostTags).ThenInclude(t => t.Tag).ToList();
You are doing a many-to-many with what is known as Payload class. The PostTag class is technically not needed for EF to create a many-to-many relationship.
You are going to have to do something like this:
var results = _context.Posts.Include(s => s.PostTags.Select(pt => pt.Tag).ToList();
Right now your linq is loading the related entities, which are just the id's so it's of no use.
If your PostTags class is not going to have any other fields/properties you should look into creating a many-to-many without the payload
I am using EF7 and have a scenario which needs a many to many relationship.
I have a ParticipantSIR entity and a ParticipantAssessmentReport entity. There is a link table ParticipantSIRAssessmentReport between them.
public class ParticipantSIR
{
public int ParticipantSIRID { get; set; }
public virtual ICollection<ParticipantSIRAssessmentReport> ParticipantSIRAssessmentReport { get; set; }
public virtual Participant Participant { get; set; }
}
public class ParticipantAssessmentReport
{
public int ParticipantAssessmentReportID { get; set; }
public virtual ICollection<ParticipantSIRAssessmentReport> ParticipantSIRAssessmentReport { get; set; }
}
public partial class ParticipantSIRAssessmentReport
{
public int ParticipantSIRID { get; set; }
public int ParticipantAssessmentReportID { get; set; }
public virtual ParticipantAssessmentReport ParticipantAssessmentReport { get; set; }
public virtual ParticipantSIR ParticipantSIR { get; set; }
}
modelBuilder.Entity<ParticipantSIRAssessmentReport>(entity =>
{
entity.HasKey(e => new { e.ParticipantSIRID, e.ParticipantAssessmentReportID });
entity.HasOne(d => d.ParticipantAssessmentReport).WithMany(p => p.ParticipantSIRAssessmentReport).HasForeignKey(d => d.ParticipantAssessmentReportID).OnDelete(DeleteBehavior.Restrict);
entity.HasOne(d => d.ParticipantSIR).WithMany(p => p.ParticipantSIRAssessmentReport).HasForeignKey(d => d.ParticipantSIRID).OnDelete(DeleteBehavior.Restrict);
});
This appears to be the way this needs to be setup with EF core including the third entity. I got some of the information from. http://ef.readthedocs.io/en/latest/modeling/relationships.html#many-to-many
When I insert data the 2 outside entities get populated but not the link table.
Since there are no navigation properties between the ParticipantSIR and ParticipantAssessmentReport then I'm not sure how to add the linked data.
_db.ParticipantAssessmentReport.Add(participantAssessmentReport);
foreach (var sir in participantSirs)
{
_db.ParticipantSIR.Add(sir);
}
_db.SaveChanges();
Assuming we're talking about EF Core 1.0rc1 it looks like you have created your model correctly (except the virtual keyword doesn't do anything yet as lazy loading hasn't been implemented).
As many-to-many hasn't been implemented yet as of 1.0rc1 you need to do some extra work. See https://github.com/aspnet/EntityFramework/issues/1368#issuecomment-180066124 for the classic blog Post, Tag, PostTag example code.
In your case you need to explictly add to ParticipantSIRAssessmentReport, something like this:
var participantSIRAssessmentReport = new ParticipantSIRAssessmentReport {ParticipantSIR = participantSIR, ParticipantAssessmentReport = participantAssessmentReport };
_db.ParticipantSIRAssessmentReport.Add(participantSIRAssessmentReport);
_db.SaveChanges();
To map Many-To-Many relationships in EF you need to add the following to your DbContext's OnModelCreating() method:
modelBuilder.Entity<ParticipantSIR>()
.HasMany(e => e.ParticipantSIRAssessmentReport)
.WithMany(e => e.ParticipantSIR)
.Map(e => e.ToTable("ParticipantSIRAssessmentReport") //Name of the linking table
.MapLeftKey("ParticipantSIRId") //Name of the Left column
.MapRightKey("ParticipantSIRAssessmentReportId")); //Name of the right column
From here the relationship will be handled using the Collections inside each of the classes.
So following this example:
http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx
I have a Student class:
public class Student
{
public Student() { }
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
and a Course class
public class Course
{
public Course()
{
this.Students = new HashSet<Student>();
}
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
They have Many-to-Many relationship which is configured this way:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().HasMany<Course>(s => s.Courses).WithMany(c => c.Students).Map(c =>
{
c.MapLeftKey("Student_id");
c.MapRightKey("Course_id");
c.ToTable("StudentAndCourse");
});
base.OnModelCreating(modelBuilder);
}
I have created the models and tables myself and map to them. So far so good. The problem is I do not need the collection of related Students to my courses. In other words when I get my courses they are coming with their related Students. I need the Students with the assigned Courses but when I get the courses through my repo, I need only them.
I tried to remove the Students collection from my Course class but was enable to fix the mapping. I am not experienced with EF and any help with working example will be greatly appreciated.
There is WithMany() method which does not require navigation property for related entities:
modelBuilder.Entity<Student>()
.HasMany(s => s.Courses)
.WithMany()
.Map(c =>
{
c.MapLeftKey("Student_id");
c.MapRightKey("Course_id");
c.ToTable("StudentAndCourse");
});
I have a sample code:
public class Tag
{
public int TagId { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public class User
{
public int UserId { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
When I run EF over my model (i use code first approach), i get some tables automatically created in my db:
Users
Tags
UserTagUsers <-- junction table for many-to-many relationship
It is okay, till I decide to add one more property to User entity:
public class User
{
public int UserId { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<Tag> Tags2 { get; set; }
}
in this case EF generates completely different relations, it removes UserTagUsers junction table, but adds some additional properties to Tags table in order to make it one-to-one mapping.
How can I explicitly tell EF to make the property Tags and Tags2 to be many-to-many?
Use fluent API to configure the mappings
modelBuilder.Entity<User>()
.HasMany(u => u.Tags).WithMany(t => t.Users)
.Map(m =>
{
m.ToTable("UserTags");
m.MapLeftKey("UserId");
m.MapRightKey("TagId");
});
modelBuilder.Entity<User>()
.HasMany(u => u.Tags2).WithMany(t => t.Users2)
.Map(m =>
{
m.ToTable("UserTags2");
m.MapLeftKey("UserId");
m.MapRightKey("TagId");
});