I've been struggling with this for a few days.
I have three models that link together with many to many relationships.
Rules:
A requirement can have many controls and vice versa
A procedure can have many controls and vice versa
I am currently showing all controls linked to requirements in my requirements views without a problem and i've even got the create / update working through the creation of viewmodels that hold the assigned data
I'd like to show the list of all procedures that are linked to the controls which are linked to the requirement I am viewing. I won't want to edit them at that level as that will be done through the control Controller. It's a link through two join tables that i'm unable figure out :(
Models:
public class Requirement
{
[Key]
public int RequirementId { get; set }
public string Name { get; set; }
public string Details { get; set; }
public virtual ICollection<Control> Controls { get; set; }
}
public class Control
{
public int ControlId { get; set; }
public string Name { get; set; }
public virtual ICollection<Requirement> Requirements { get; set; }
public virtual ICollection<Procedure> Procedures { get; set; }
}
public class Procedure
{
[Key]
public int ProcedureId { get; set; }
public string Name { get; set; }
public string Details { get; set; }
public virtual ICollection<Control> Controls { get; set; }
}
Dbcontext:
public class CompliancePortalContext : DbContext
{
public CompliancePortalContext()
: base("CompliancePortalContext")
{ }
public DbSet<Control> Controls { get; set; }
public DbSet<Procedure> Procedures { get; set; }
public DbSet<Requirement> Requirements { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Procedure>().HasMany(c => c.Controls).WithMany(p => p.Procedures).Map(
mc =>
{
mc.MapLeftKey("ProcedureId");
mc.MapRightKey("ControlId");
mc.ToTable("ProcedureControl");
});
modelBuilder.Entity<Requirement>().HasMany(c => c.Controls).WithMany(r => r.Requirements).Map(
mc =>
{
mc.MapLeftKey("RequirementId");
mc.MapRightKey("ControlId");
mc.ToTable("RequirementControl");
});
base.OnModelCreating(modelBuilder);
}
}
This should give you all the procedures linked to all the controls linked to all requirements.
from r in Requirements
from c in r.Controls
from p in c.Procedures
select p
Related
I have two entities in my MVC application and I populated the database with Entity Framework 6 Code First approach. There are two city id in the Student entity; one of them for BirthCity, the other for WorkingCity. When I define the foreign keys as above an extra column is created named City_ID in the Student table after migration. Id there a mistake or how to define these FKs? Thanks in advance.
Student:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int BirthCityID { get; set; }
public int LivingCityID { get; set; }
[ForeignKey("BirthCityID")]
public virtual City BirthCity { get; set; }
[ForeignKey("LivingCityID")]
public virtual City LivingCity { get; set; }
}
City:
public class City
{
public int ID { get; set; }
public string CityName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
To achieve what you want you need to provide some aditional configuration.Code First convention can identify bidirectional relationships, but not when there are
multiple bidirectional relationships between two entities.You can add configuration (using Data Annotations or the Fluent API) to present this
information to the model builder. With Data Annotations, you’ll use an annotation
called InverseProperty. With the Fluent API, you’ll use a combination of the Has/With methods to specify the correct ends of these relationships.
Using Data Annotations could be like this:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int BirthCityID { get; set; }
public int LivingCityID { get; set; }
[ForeignKey("BirthCityID")]
[InverseProperty("Students")]
public virtual City BirthCity { get; set; }
[ForeignKey("LivingCityID")]
public virtual City LivingCity { get; set; }
}
This way you specifying explicitly that you want to relate the BirthCity navigation property with Students navigation property in the other end of the relationship.
Using Fluent Api could be like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().HasRequired(m => m.BirthCity)
.WithMany(m => m.Students).HasForeignKey(m=>m.BirthCityId);
modelBuilder.Entity<Student>().HasRequired(m => m.LivingCity)
.WithMany().HasForeignKey(m=>m.LivingCityId);
}
With this last solution you don't need to use any attibute.
Now, the suggestion of #ChristPratt in have a collection of Student in your City class for each relationship is really useful. If you do that, then the configurations using Data Annotations could be this way:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int BirthCityID { get; set; }
public int LivingCityID { get; set; }
[ForeignKey("BirthCityID")]
[InverseProperty("BirthCityStudents")]
public virtual City BirthCity { get; set; }
[ForeignKey("LivingCityID")]
[InverseProperty("LivingCityStudents")]
public virtual City LivingCity { get; set; }
}
Or using Fluent Api following the same idea:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().HasRequired(m => m.BirthCity)
.WithMany(m => m.BirthCityStudents).HasForeignKey(m=>m.BirthCityId);
modelBuilder.Entity<Student>().HasRequired(m => m.LivingCity)
.WithMany(m => m.LivingCityStudents).HasForeignKey(m=>m.LivingCityId);
}
Sheesh. It's been a long day. There's actually a very big, glaring problem with your code, actually, that I completely missed when I commented.
The problem is that you're using a single collection of students on City. What's actually happening here is that EF can't decide which foreign key it should actually map that collection to, so it creates another foreign key specifically to track that relationship. Then, in effect you have no navigation properties for the collections of students derived from BirthCity and LivingCity.
For this, you have to drop down to fluent configuration, as there's no way to configure this properly using just data annotations. You'll also need an additional collection of students so you can track both relationships:
public class City
{
...
public virtual ICollection<Student> BirthCityStudents { get; set; }
public virtual ICollection<Student> LivingCityStudents { get; set; }
}
Then, for Student:
public class Student
{
...
public class StudentMapping : EntityTypeConfiguration<Student>
{
public StudentMapping()
{
HasRequired(m => m.BirthCity).WithMany(m => m.BirthCityStudents);
HasRequired(m => m.LivingCity).WithMany(m => m.LivingCityStudents);
}
}
}
And finally in your context:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new Student.StudentMapping());
}
As far as i know, i have two way to implement many-to-many relation in asp.net mvc using code-first.
1- Fluent Api
public class HrPerson
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrPersonTitle> HrPersonTitle { get; set; }
}
public class HrPersonTitle
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<HrPerson> HrPerson { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<HrPerson>()
.HasMany(s => s.HrPersonTitle)
.WithMany(c => c.HrPerson)
.Map(t =>
{
t.MapLeftKey("HrPersonId")
.MapRightKey("HrPersonTitleId")
.ToTable("HrMapPersonTitle");
});
}
2-Custom Mapping Table
public class HrPerson
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrMapPersonTitle> HrMapPersonTitle { get; set; }
}
public class HrPersonTitle
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<HrMapPersonTitle> HrMapPersonTitle { get; set; }
}
public class HrMapPersonTitle
{
public int Id { get; set; }
public int HrPersonId { get; set; }
public int HrPersonTitleId { get; set; }
public virtual HrPerson HrPerson { get; set; }
public virtual HrPersonTitle HrPersonTitle { get; set; }
public string Note { get; set; }
public bool Deleted { get; set; }
}
My questions:
If i choose second way, i am not able to reach HrPersonTitle.Name property from HrPerson model in the view. How can i reach the properties ?
If i choose the first way i can reach the HrPersonTitle.Name but i am not able to add more property in the map file ? How can i add more properties?
Regards.
When you create a M2M without a payload (just the foreign key relationships, no extra data), EF collapses the relationship so that you can query directly without having to explicitly go through the join table. However, if you need a payload, then EF can no longer manage the relationship in this way.
So, if you want to get the title, you have to go through HrMapPersonTitle:
#foreach (var title in Model.HrMapPersonTitle)
{
#title.HrPersonTitle.Name
}
Both these methods seem overkill maybe. I don't know your full intentions however I implement this all the time at work and I use the following:
public class HrPerson
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrPersonTitle> HrPersonTitles { get; set; }
}
public class HrPersonTitle
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<HrPerson> HrPersons { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<HrPerson>()
.HasMany(s => s.HrPersonTitles)
.WithMany(c => c.HrPersons);
}
If you are using code first and you try and access either mapping within the DbContext it should Lazy Load your information and every property should be accessible.
I do have one question though. Are you sure it should be many to many, do they really have multiple titles?
I am using MVC 4 with EF code first approach. I have two simple objects. These are their POCO classes:
public class Activity
{
//Primitive Properties
[HiddenInput]
public int Id { get; set; }
[Required]
public int LengthInMinutes { get; set; }
public string AdditionalInfo { get; set; }
[Required]
public bool Archive { get; set; }
//Navigation Properties
public virtual User User { get; set; }
public virtual ActivitySet ActivitySet { get; set; }
public virtual ICollection<Company> Companies { get; set; }
public virtual ICollection<Description> Descriptions { get; set; }
}
public class Company
{
//Primitive Properties
[HiddenInput]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public bool Archive { get; set; }
//Navigation Properties
public virtual ICollection<Activity> Activities { get; set; }
}
Now, I have generic list of activities which I iterate through using foreach loop. While looping I want to write a name for each Company related to the activity from the list. This is a code I came up with:
#foreach (Activity a in Model)
{
<p>#a.Companies.Where(d => d.Activities.FirstOrDefault(y => y.Id == a.Id)).Single()</p>
}
Unfortunately it gives me compilation error when I build the project. How can I then access details of the elements with many-to-many relationship
You could try it like so:
#foreach (Activity a in Model)
{
<p>#string.Join(",", a.Companies.Select(c => c.Title))</p>
}
It should give a list of all company titles of a given activity separated by a comma.
I'm trying to create a list of train journeys (among other things) in MVC, using code first Entity Framework and wondered how I could map foreign keys for the stations. The Journey model/table will have a DepartureStationID and an ArrivalStationID which will be foreign keys linking to one table/model, called Station.
Here is the code for both these models:
public class Station
{
public int StationID { get; set; }
public string StationName { get; set; }
public string StationLocation { get; set; }
}
public class Journey
{
public int JourneyID { get; set; }
public int DepartureID { get; set; }
public int ArrivalID { get; set; }
public int OperatorID { get; set; }
public string JourneyCode { get; set; }
public virtual Operator Operator { get; set; }
public virtual Station DepartureStation { get; set; }
public virtual Station ArrivalStation { get; set; }
}
There is another foreign key value in there, namely Operator and that has mapped successfully, but the departure and arrivals haven't, and return null values in the view: (#Html.DisplayFor(modelItem => item.DepartureStation.StationName).
When I looked in the database, there had been two additional fields created by EF:
DepartureStation_StationID
ArrivalStation_StationID
And the SQL relationship was between the station table and the two fields above, rather than DepartureID and ArrivalID
So, my question is - Do I need to do something different in the model when referencing the same table for two fields? I don't know why those additional fields were added so I presume I've set up the model incorrectly.
Thanks
For completeness, here's the same thing with fluent configuration.
public class MyDb : DbContext
{
public DbSet<Journey> Journeys { get; set; }
public DbSet<Operator> Operators { get; set; }
public DbSet<Station> Stations { get; set; }
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Entity<Journey>()
.HasRequired(j => j.DepartureStation)
.WithMany()
.HasForeignKey(j => j.DepartureID);
builder.Entity<Journey>()
.HasRequired(j => j.ArrivalStation)
.WithMany()
.HasForeignKey(j => j.ArrivalId);
// ... Same thing for operator ...
base.OnModelCreating(builder);
}
}
Edit: To address your above comment about the cascade delete, you can add .WillCascadeOnDelete(false) after .HasForeignKey() and that might help (although you'll then have to delete Journey records manually)
Add the folowing attributes on your navigation properties :
public class Journey
{
public int JourneyID { get; set; }
public int DepartureID { get; set; }
public int ArrivalID { get; set; }
public int OperatorID { get; set; }
public string JourneyCode { get; set; }
[ForeignKey("OperatorID")]
public virtual Operator Operator { get; set; }
[ForeignKey("DepartureID")]
public virtual Station DepartureStation { get; set; }
[ForeignKey("ArrivalID")]
public virtual Station ArrivalStation { get; set; }
}
And of course you need to regenerate your database in order to apply the new configuration.
Hope this will help.
I have the following business role that I need to model:
A bidder could rate a seller as long as they've interacted with this person
A bidder could rate an item only if he had won the auction
The final rating for the seller though, is the average taken from the item rating and the others' ratings on himself.
The rating itself (whether for the item or the user) is the average of scores on several questions.
Accordingly, I thought I should create a Ratings class, then inherit it with UserRating and ItemRating. Both of those should have an ICollection of RatingQuestion (which will eventually be a static table). The questions for the UserRating are different from those of the ItemRating, but I thought it's not really worth creating separate tables/entities for the questions (or maybe I should do a TPH inheritance?).
So, here's what I got so far:
public abstract class Rating
{
public int Id { get; set; }
public virtual User By { get; set; }
}
public class UserRating : Rating
{
public virtual User For { get; set; }
public virtual ICollection<RatingQuestion> Questions { get; set; }
}
public class ItemRating : Rating
{
public virtual Item For { get; set; }
public virtual ICollection<RatingQuestion> Questions { get; set; }
}
public class RatingQuestion
{
public int Id { get; set; }
public string Text { get; set; }
public virtual ICollection<Rating> Rating { get; set; }
}
The reason why I am putting the ICollection inside the sub-classes rather than the Rating base class is because the RatingQuestion for both is different, but I'm not sure that's the way I should be doing it, correct me if I'm wrong please.
One thing I need some help with is deciding whether to go for a TPH or a TPT inheritance. I want to keep things simple, but I would also want to keep my database normalized. Moreover, performance is a factor that needs to be taken into account.
Now the last thing I need to know how to do is: how to map the many-to-many relationship between the rating classes (the base class or sub-classes, not sure about which one I should be using) and the RatingQuestion class using the Fluent API AND add an attribute (score) which is a property of the relationship itself so I could record the score on every separate RatingQuestion.
I hope that was clear enough. All suggestions are most welcome. Thanks in advance!
UPDATE: (after Ladislav Mrnka's answer)
public abstract class Rating
{
public int Id { get; set; }
public virtual User By { get; set; }
public virtual ICollection<RatingQuestion> RatingQuestions { get; set; }
}
public class UserRating : Rating
{
public virtual User For { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
public class ItemRating : Rating
{
public virtual Item For { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
public class User
{
public int Id { get; set; }
//more properties
public virtual ICollection<UserRating> OwnRatings { get; set; }
public virtual ICollection<UserRating> RatingsForOthers { get; set; }
public virtual ICollection<ItemRating> ItemRatings { get; set; }
}
public class Item
{
public int Id { get; set; }
//more properties
public virtual ItemRating Rating { get; set; } //because an Item will have only one rating
}
public class UserRatingConfiguration : EntityTypeConfiguration<UserRating>
{
public UserRatingConfiguration()
{
HasOptional(p => p.By)
.WithMany(u => u.RatingsForOthers)
.IsIndependent()
.Map(m => m.MapKey(c => c.Id, "RatingSubmitter"));
HasRequired(p => p.For)
.WithMany(u => u.OwnRatings)
.IsIndependent()
.Map(m=>m.MapKey(c => c.Id, "RatedSeller"));
}
}
public class ItemRatingConfiguration : EntityTypeConfiguration<ItemRating>
{
public ItemRatingConfiguration()
{
HasRequired(p => p.By)
.WithMany(u => u.ItemRatings)
.IsIndependent()
.Map(m=>m.MapKey(c => c.Id, "ItemRatingSubmitter"));
}
}
I'm getting a very messed up model in SQL Server, which is obviously caused by my messed up mapping. Any suggestions or should I just forget about inheritance and the DRY principle all together in the case at hand?
You can't use direct M:N mapping if you need to add custom property to that relation. In such case you need to model junction table as another entity which will hold reference to Rating and Question and also include Score property.
I would recommend using TPH inheritance. It is easier to use and it has better performance. TPT constructs really ugly queries. Also there is no reason to have RatingQuestions in derived classes. Both these collections reference same type so you can move it to parent. Moreover according to this question there are some problems with navigation properties in child classes when using TPH. I'm not sure if this problem is still valid in Code-first. Anyway your current model simply don't need navigation property on child.
If you follow my advices you don't need to add any mapping. It will map with default conventions when using these classes:
public abstract class Rating
{
public virtual int Id { get; set; }
public virtual User By { get; set; }
private ICollection<RatingQuestion> _ratingQuestions = null;
public ICollection<RatingQuestion> RatingQuestions
{
get
{
if (_ratingQuestions == null)
{
_ratingQuestions = new HashSet<RatingQuestion>();
}
return _ratingQuestions;
}
protected set { _ratingQuestions = value; }
}
}
public class ItemRating : Rating
{
public virtual Item For { get; set; }
}
public class UserRating : Rating
{
public virtual User For { get; set; }
}
public class RatingQuestion
{
public virtual int Id { get; set; }
public virtual int Score { get; set; }
public virtual Rating Rating { get; set; }
public virtual Question Question { get; set; }
}
public class Question
{
public virtual int Id { get; set; }
public virtual string Text { get; set; }
private ICollection<RatingQuestion> _ratingQuestions = null;
public ICollection<RatingQuestion> RatingQuestions
{
get
{
if (_ratingQuestions == null)
{
_ratingQuestions = new HashSet<RatingQuestion>();
}
return _ratingQuestions;
}
protected set { _ratingQuestions = value; }
}
}