I have the following scenario in my project and I'm starting to get with a few questions about it.
On the User class, querying QuestionFollows directly will be better (in terms of performance) than querying Questions and then QuestionFollows?
Another question ... is the relationship between User and QuestionFollow necessary/redudant?
Domain
public class User
{
public long UserID { get; set; }
public string Name { get; set; }
public virtual ICollection<Question> Questions { get; set; }
public virtual ICollection<QuestionFollow> QuestionFollows { get; set; }
}
public class Question
{
public long QuestionID { get; set; }
public long UserID { get; set; }
public string Description { get; set; }
public User User { get; set; }
public virtual ICollection<QuestionFollow> QuestionFollows { get; set; }
}
public class QuestionFollow
{
public long QuestionFollowID { get; set; }
public long QuestionID { get; set; }
public long UserID { get; set; }
public DateTime Date { get; set; }
public Question Question { get; set; }
public User User { get; set; }
}
Everything you're doing seems right based on assumptions I gather from your code: a user has questions and can also follow questions that belong to other users. If that's correct, then it seems you're simply struggling with how these relationships play out.
From User, querying Questions will return all questions belonging to that User instance. Querying QuestionFollows, however, will return all questions that User has chosen to follow, whether or not those questions belong to that User instance. In other words, these are two functionally different datasets, so it's not about which is more performant; it's about which returns the data you actually need.
The relationship between User and QuestionFollows is also not redundant, because again you're tracking a fundamentally different facet of the relationship. In QuestionFollow the User is the entity that is following the question and the Question is the entity that is being followed. That Question entity could have a completely different User attached to it, as that User entity is the one who owns the question.
Related
I'm trying to map a relation N:N for an entity which has some other information. In fact, to brief you better I have the following scenario:
A user can apply as many times as he wants for a exam and this application saves the final result. (That's why i didn't map the key with this to classes)
Looking for this over the internet I found some solutions regarding the creation of Id properties to save information about the Foreign Key besides the property itself. As I don't agree with this solution because I don't believe that we have to change our Model to satisfy ORM needs, I would like to know if you guys have another solution for me.
The following code is a piece of the classes I want to map. Currently, I didn't configure collections in the main classes and when I try to save the application I receive a key violation in the database because it tries to save the User/Exam in the database again.
Sorry if it is a silly question and thanks for your help.
public class User
{
public int Id { get; set; }
public string FullName { get; set; }
}
public class Exam
{
public int Id { get; set; }
public string Description { get; set; }
}
public class Application
{
public int Id { get; set; }
public virtual User User { get; set; }
public virtual Exam Exam { get; set; }
public int Result { get; set; }
}
I have a Code First MVC 4 Web app - there's a many to many relationship between Places:
public virtual int PlaceID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<User> Followers { get; set; }
and Users:
public virtual int UserID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Place> Places { get; set; }
Users can 'follow' many places, places can have many followers.
I would like to store additional information about the relationship - say a rating. So a user could have a rating for each place they follow, of between 1-10, say.
How can I achieve that - including CRUD operations (Code First, Entity Framework 5).
Thanks.
You will need to replace the many-to-many with two one-to-many relationships to an intermediate class that has the rating property which could be defined as below (you may need to add navigation attributes):
Create an intermediate class
public class UserPlaceRelationship {
public virtual int PlaceID { get; set; }
public virtual int UserID { get; set; }
public virtual Place Place { get; set; }
public virtual User User { get; set; }
public virtual int Rating { get; set; }
}
Update your places class:
public virtual int PlaceID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<UserPlaceReationship> UserRelationships { get; set; }
and your users class:
public virtual int UserID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<UserPlaceReationship> PlaceRelationships { get; set; }
Querying will be a little trickier as you will have to now navigate through the intermediate class.
You must create a junction table and add extra info to it.
Till now, there is no way to create a CRUD master-detail controller with related views.
You should follow the answer I provided to this question of yours...
I am trying to use scaffold my models.
My Model looks like this
public class Patient
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual int MRN { get; set; }
public virtual ICollection<Issue> Issues { get; set; }
}
public class Issue
{
public virtual int Id { get; set; }
public virtual int PatientId { get; set; }
public virtual Patient Patient { get; set; }
public virtual string Room { get; set; }
public virtual string Comment { get; set; }
}
So I would like it to be when the user adds a patient then the Create Scaffold would show the fields for the Patient and also for the Issue. What I am getting now is just the Patient fields. Is there a way to go about this?
I would like to give the option when creating a new patient, the customer gets the ability to add multiple Issues for the patient.
the default scaffolding stuff in MVC3, and even 4, to my knowledge will not handle this. there are too many different and complex variables for scaffolding to really be reliable in my opinion. you'll need to wire this up yourself. i would suggest creating an editor for your types. that will simplify things at least a little bit.
I have an entity User. Each User is supposed to have many Friends and Teachers. With EF Code First I am a little bit confused on how to achieve what I want. I saw examples of self reference, but not many-to-many. For example:
public class Employee
{
#region Properties
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? ManagerID { get; set; }
public Employee Manager { get; set; }
#endregion
}
and the modelBuilder:
modelBuilder.Entity<Employee>().
HasOptional(e => e.Manager).
WithMany().
HasForeignKey(m => m.ManagerID);
How to create an entity with self-reference in my case, where there are Friends (if a is friend with b this means that b is friend with a) and Teachers (if a is teacher of b, b is student of a)?
Sorry if there already exists a similar thread.
Any help is greatly appreciated.
Ok, so because a lot of people answered this ;), I searched a little more and I found the solution for my case. The entity looks like:
public class User
{
public int ID { get; set; }
public string UserName { get; set; }
public virtual ICollection<User> Friends { get; set; }
}
The modelBuilder:
modelBuilder.Entity<User>()
.HasMany(x => x.Friends)
.WithMany();
And finally I made helper methods BeFriend and BecomeTeacherStudent, which make sure that when you become friend with someone he becomes friend with you (and the respective thing for teacher-student).
I am building a reservation system. I have users in roles('admin', 'client', 'employee', 'student').
Each reservation must be associated with a user of role client, it might be assigned to user of role employee and might also be assigned to user of role student.
So in my reservation class I have properties of type User and I have marked them with [ForeignKey("AnytypeId")] attribute to hint EF for relations.
I have seen code like this at http://blog.stevensanderson.com/2011/01/28/mvcscaffolding-one-to-many-relationships/
public class Reservation
{
public int ReservationID
{
get;
set;
}
[Required(ErrorMessage="Please provide a valid date")]
public DateTime ReservationDate
{
get;
set;
}
public DateTime ReservationEnd { get; set; }
public DateTime EntryDate
{
get;
set;
}
public DateTime UpdatedOn
{
get;
set;
}
public decimal Ammount
{
get;
set;
}
public decimal? Discount { get; set; }
[DataType(DataType.MultilineText)]
public string ServiceDetails { get; set; }
[DataType(DataType.MultilineText)]
public string Remarks { get; set; }
public String PaymentMethod { get; set; }
public string VoucherNumber { get; set; }
public int ServiceID
{
get;
set;
}
public virtual Service Service
{
get;
set;
}
public string EmployeeID { get; set; }
[ForeignKey("EmployeeID")]
public virtual User Employee { get; set; }
public string ClientID { get; set; }
[ForeignKey("ClientID")]
public virtual User Client { get; set; }
public string StudentID { get; set; }
[ForeignKey("StudentID")]
public virtual User Student { get; set; }
}
public class ReservationMap : EntityTypeConfiguration<Reservation>
{
public ReservationMap()
{
this.HasOptional(r => r.Client).WithMany().WillCascadeOnDelete(true);
this.HasOptional(r => r.Employee).WithMany().WillCascadeOnDelete(false);
this.HasOptional(r=>r.Student).WithMany().WillCascadeOnDelete(false);
}
}
Now as I run my mvc3 EF code first app database created for me on the fly with following ERD and edmx model.
Now few problems that I am having:
1. When I am listing all the users of role clients in view their reservation property is showing always 0 even if their are reservations available in database. I don't know why this collection property marked with virtual is not loading??
Please I am stuck with this help me out here this is the last thing remaining.
There are couple of problems in your model. You have configured the one to many relationship in such a way that the many end(Reservations property) is excluded from the mapping. Hence the Reservations will not be loaded by EF.
The other problem is if you are going to map the Reservations property as the many end of the relationship, what will be the navigational property? is it Employee, Client, Student? Because only one of these properties can participate in the relationship with Reservations property.
It is not clear as to how these relationships should be modeled by your description. One way would be to have 3 collection properties.
public class ReservationMap : EntityTypeConfiguration<Reservation>
{
public ReservationMap()
{
HasOptional(r => r.Client).WithMany(u => u.ClientReservations).WillCascadeOnDelete(true);
HasOptional(r => r.Employee).WithMany(u => u.EmployeeReservations).WillCascadeOnDelete(false);
HasOptional(r=>r.Student).WithMany(u => u.StudentReservations).WillCascadeOnDelete(false);
}
}