Implementing Many-To-Many relationship in Asp.Net (confused?) - asp.net-mvc

My current aim is to build a database structure using classes in Entity Framework & ASP MVC.
I currently have a Users table and a Posts table. What I would like to do is create a many to many relationship for Users who have liked Posts (whilst conserving who created the post). And be able to access for each user all of the posts they have liked. Currently I have these classes but I'm unsure of how to link them as all of the online examples are linking Primary Keys from different databases where I just want to use the Username Parameter. Any help would be great. I have this so far.
public class Posts
{
[Key]
public virtual int PostId { get; set; }
public virtual string Title { get; set; }
public virtual string URL { get; set; }
[DisplayName("Main Text")]
public virtual string TextBody { get; set; }
public int PostLikes { get; set; }
private DateTime Datedata = DateTime.Now;
public DateTime PostDate { get { return Datedata; } set { Datedata = value; } }
public virtual Users User { get; set; }
public ICollection<PostLikes> UsersWhoHaveSigned { get; set; }
}
{
public class Users
{
[Key]
public virtual int UserId { get; set; }
public virtual string Username { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual List<Posts> Post { get; set; }
}
}
I have not built the UsersWhoHaveSigned table yet. Early experimentation caused me so much backtracing it was painful. Any help would be great.
Edit: I was hoping to ask for help and then appropriate that informtaion to fit my example which utilises the individual accounts add-on. This produces some addition files that are now causing interference with the code you've provided.
Here is the IdentityModels.cs file.
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace Coursework2.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
I believe that the assembly directives at the top are preventing system.data.entity from being used so when I try to implement ApplicationDbContext : DbContext I get error messages :/
Ideally I'm looking to use the IdentityModels.cs file as a replacement for the users class. But still very lost.

First of all, I recommend that you use the singular form for your class names, as EF will automatically pluralize table names.
Second, for a key property, you can just use the term Id, without any annotations, and EF will pick it up as the principal key.
Finally, I'll assume you are looking to use a Code-First approach. Consider the following classes (yours, but refactored for clarity purpose):
public class Post
{
public virtual Guid Id { get; set; }
public virtual string UserName { get; set; }
public virtual User User { get; set; }
public virtual ICollection<PostLike> Likes { get; set; }
}
public class PostLike
{
public virtual Guid Id { get; set; }
public virtual Guid PostId { get; set; }
public virtual Post Post { get; set; }
public virtual string UserName { get; set; }
public virtual User User { get; set; }
}
public class User
{
public virtual Guid Id { get; set; }
public virtual string UserName { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public virtual ICollection<PostLike> Likes { get; set; }
}
To make it work, you'd need a DbContext such as the following. Pay attention to the OnModelCreating method, which is where the magic happens:
public class ApplicationDbContext
: DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Post> Posts { get; set; }
public DbSet<PostLike> PostLikes { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>()
.HasAlternateKey(u => u.UserName);
modelBuilder.Entity<User>()
.HasMany(u => u.Posts)
.WithOne(p => p.User);
modelBuilder.Entity<Post>()
.HasOne(p => p.User)
.WithMany(u => u.Posts)
.HasForeignKey(p => p.UserName)
.HasPrincipalKey(u => u.UserName);
modelBuilder.Entity<Post>()
.HasMany(p => p.Likes)
.WithOne(pl => pl.Post);
modelBuilder.Entity<PostLike>()
.HasOne(pl => pl.Post)
.WithMany(p => p.Likes);
modelBuilder.Entity<PostLike>()
.HasOne(pl => pl.User)
.WithMany(u => u.Likes)
.HasForeignKey(pl => pl.UserName)
.HasPrincipalKey(u => u.UserName);
}
}
Voila! I hope it answers your question ;)
If so, please don't forget to mark my post as the answer!
Edit:
I'll provide some explanations, that I had left out to answer your question ASAP.
So, first thing you need to do, is to declare the UserName as an alternate key, because you want to create relationships depending on it, and you already have the 'Id' principal key declared.
Second, on each object that should own a User reference base on the UserName alternate key, you need to declare the object's UserName property as the foreign key of the relationship, and the User's UserName property as the principal key.
In other words, the foreign key is the property that a referencing object uses for the relationship, and the principal key is the property based on which the referenced object is bound to the referencing one.
Note that principal keys must have a key or alternate key constraint, or it won't work.

Just to comment on your answer. I found that I had to use
using Microsoft.EntityFrameworkCore and remove System.Data.Entity - This was causing the program to be confused as to which DbContext I wanted to use. Thanks!

Related

Entity framework one to many - empty virtual collection

I have this code first database
public partial class SystemWarning
{
[Key]
public long Id { get; set; }
/// <summary>
/// id of the admin that created the entry
/// </summary>
public string CreatedById { get; set; }
public virtual AspNetUser CreatedBy { get; set; }
public string AcknowledgedById { get; set; }
public virtual AspNetUser AcknowledgedBy { get; set; }
}
public partial class AspNetUser
{
public AspNetUser()
{
SystemWarnings = new HashSet<SystemWarning>();
}
[Key]
public string Id { get; set; }
public virtual ICollection<SystemWarning> SystemWarnings { get; set; }
}
And linked together as follows
modelBuilder.Entity<AspNetUser>()
.HasMany(e => e.SystemWarnings)
.WithOptional(e => e.CreatedBy)
.HasForeignKey(e => e.CreatedById).WillCascadeOnDelete(false);
For reasons that escape me at the moment, when I extract my AspNetUser, the SystemWarnings collection is always empty, even if there are systemwarnings that are linked to the AspNetUser in the database.
I have a bunch of these 1-n links, even on the same object, and the other links remain non empty, and for now I'm not seeing the difference.
#edit: here's that other object for comparison:
public partial class UserProfile: BaseObject
{
public Guid Id { get; set; }
public string OwnerId { get; set; }
public virtual AspNetUser Owner { get; set; }
}
and the mapping
modelBuilder.Entity<AspNetUser>()
.HasMany(e => e.OwnedUserProfiles)
.WithOptional(x => x.Owner)
.HasForeignKey(x => x.OwnerId);
Seems the same to me, except that the SystemWarning has a long as Id with identity specs (forgot to post that before - it's defined as follows)
modelBuilder.Entity<SystemWarning>().Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
And that SystemWarning actually has another reference to the AspNetUser that I now added (AcknowledgedBy(Id)) which is linked as follows
modelBuilder.Entity<AspNetUser>()
.HasMany(e => e.SystemWarnings)
.WithOptional(e => e.AcknowledgedBy)
.HasForeignKey(e => e.AcknowledgedById).WillCascadeOnDelete(false);
I'll post the SQL trace soon...
Well, that edit did the trick.. both links from SystemWarning to AspNetUser are mapped on the SystemWarnings collection.. no wonder that's not working the way it's supposed to.
Note to myself.. map every 1:n to a different collection or there'll be trouble.

Entity Framework several One-to-Many with same model

My Order class
public class Order
{
[Key]
public int ID { get; set; }
(...)
[ForeignKey("Client")]
public string ClientID { get; set; }
public virtual ApplicationUser Client { get; set; }
[ForeignKey("Trader")]
public string TraderID { get; set; }
public virtual ApplicationUser Trader { get; set; }
[ForeignKey("Driver")]
public string DriverID { get; set; }
public virtual ApplicationUser Driver { get; set; }
}
And my MS Identity ApplicationUser class:
public class ApplicationUser : IdentityUser
{
(...)
public virtual List<Order> Orders { get; set; }
}
As you can see I'd like to have speciffic users in speciffic "role" in Order model. How should I write a code in ApplicationUser to get speciffic lists of Clients, Traders and Drivers? I mean, I'd like to find user in database and then I'd like to have three lists named e.g. AsClient, AsTrader and AsDriver. Right now List<Orders> count is always 0.
Use InversePropertyAttribute:
public class ApplicationUser : IdentityUser
{
//another stuff...
[InverseProperty("Client")]
public virtual ICollection<Order> AsClient { get; set; }
[InverseProperty("Trader")]
public virtual ICollection<Order> AsTrader { get; set; }
[InverseProperty("Driver")]
public virtual ICollection<Order> AsDriver { get; set; }
}
I would suggest you use a little inheritance here.
So you would create three classes that extend ApplicationUser, one for each Trader, Client and Driver. And then on each of these classes have the orders list. Also, on the Order class, change the types from ApplicationUser to the appropriate subtype.
Then as an inheritance strategy you can choose the table per hierarchy and follow the instructions on this link to implement it.
Hope this helps.

Validation error during model generation in one-to-many relationship

When I run application I have this error:
PossibleAnswer_Question_Source: : Multiplicity is not valid in Role
'PossibleAnswer_Question_Source' in relationship
'PossibleAnswer_Question'. Because the Dependent Role properties are
not the key properties, the upper bound of the multiplicity of the
Dependent Role must be '*'.
How to resolve it?
Model classes for Question and PossibleAnswer:
public class Question
{
public int ID { get; set; }
public string Text { get; set; }
public bool IsAssociatedWithProfessor { get; set; }
public bool IsAssociatedWithAssistant { get; set; }
public virtual ICollection<PossibleAnswer> PossibleAnswers { get; set; }
}
public class PossibleAnswer
{
public int ID { get; set; }
public string Text { get; set; }
public int QuestionID { get; set; }
[ForeignKey("QuestionID")]
public virtual Question Question { get; set; }
}
And I put this in OnModelCreating(DbModelBuilder modelBuilder):
modelBuilder.Entity<PossibleAnswer>()
.HasRequired(f => f.Question)
.WithRequiredDependent()
.WillCascadeOnDelete(false);
The problem is you are not configuring a one-to-many relationship in the OnModelCreating method (that is a one-to-one configuration). To achieve what you want, you could do this:
modelBuilder.Entity<PossibleAnswer>()
.HasRequired(pa => pa.Question)
.WithMany(q=>q.PossibleAnswers)
.HasForeignKey(pa=>pa.QuestionID)
.WillCascadeOnDelete(false);
This way, you don't need to use the ForeignKey attribute on the Question navigation property. Is a good practice try to not merge Fluent Api with Data Annotations

Defining multiple Foreign Key for the Same table in Entity Framework Code First

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());
}

EF6 I have GUID for UserId Stored in a table but want to add ApplicationUser to the Model

My Application is all fine and within IdentityModels I set each class (table). But I want to show in my Razor View the UserName from AspNetUsers and not the GUID. I currently store the GUID but thats all i'm able to display in the views
I'm thinking there must be a built in easy way to do this - and that I don't need to do any mapping or do i
I'm using EF6 and MVC4.5
Here is my class :
public partial class x23BatchImport
{
public int x23BatchImportId { get; set; }
public Nullable<DateTime> DateTimeFromFilename { get; set; }
public string UserId { get; set; }
public string Filename { get; set; }
public decimal? Length { get; set; }
public Nullable<DateTime> StartDateTime { get; set; }
public Nullable<DateTime> StopDateTime { get; set; }
//public virtual ApplicationUser ApplicationUser { get; set; }
}
...here is an extract from IdentityModels.cs
namespace AscendancyCF.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
//public System.Data.Entity.DbSet<AscendancyCF.Models.ApplicationUser> ApplicationUser { get; set; }
public System.Data.Entity.DbSet<AscendancyCF.Models.SupplyPointType> SupplyPointTypes { get; set; } ETC ETC
.....NB all my tables are declared here then I use OnModelCreating to set up relationships some of the time...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Reference : http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx
base.OnModelCreating(modelBuilder);
// Configure the 1-1
modelBuilder.Entity<SupplyPoint>()
.HasOptional(a => a.SupplyPointAddress)
.WithMany()
.HasForeignKey(u => u.SupplyPointAddressId);
}
Entity Framework has a set of model and property naming conventions that it uses by default. Currently, it's not able to figure out that UserId is a foreign key to an ApplicationUser.
If you don't want to add any manual mappings, you have to change your naming. The simplest would be to rename the ApplicationUser property to User.
public virtual ApplicationUser User { get; set; }
When doing your queries, use Include() to eager load the User property with the matching ApplicationUser...
// using System.Data.Entity; // add this using to use Include()
var context = new ApplicationDbContext();
var batchImport = context.x23BatchImport
.Include(x => x.User)
.(x => x.x23BatchImportId == 1)
.Single();
var username = batchImport.User.UserName;
Your other alternatives are:
Change UserID property to ApplicationUserId
Specify the foreign key in a manual mapping in OnModelCreating()
Specify the foreign key on the model using data annotations

Resources