Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User in Entity type MVC5 EF6

I have created a class in MVC5, where I want a primary owner of the content and then I want to have some editors for the content:

public class Content {     public int ID { get; set; }     public IdentityUser Owner { get; set; }     public ICollection<IdentityUser> Editors { get; set; }     public string Title{ get; set; }     public string Body { get; set; } } 

In the database context I have the following code:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {     base.OnModelCreating(modelBuilder);      modelBuilder.Entity<Content>()         .HasOptional(c => c.Editors)         .WithRequired()         .WillCascadeOnDelete();      modelBuilder.Entity<Content>()         .HasRequired(c => c.Owner)         .WithOptional()         .WillCascadeOnDelete(); } 

I would expect Microsoft to have implemented the IdentityUser object in such a way, that it can be used in other entity types, so I am probably doing something wrong, because when I try to make a controller for the Content entity type, I get the following error:

EntityType 'IdentityUserLogin' has no key defined EntityType 'IdentityUserRole' has no key defined EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no key defined EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no key defined 

I have also tried to add the following code to the ApplicationDbContext as described in this question Map tables using fluent api in asp.net MVC5 EF6?:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {     base.OnModelCreating(modelBuilder);     modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);     modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);     modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId }); } 

I must be doing something wrong. Please tell me how I can handle users in my Content EntityType.

like image 735
Gunnar Avatar asked Nov 11 '13 18:11

Gunnar


People also ask

How do I use code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

What is DbSet in Entity Framework?

A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext. Set method.

What is DbContext in Entity Framework?

A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.

What is entity model in MVC?

What is Entity framework? Entity framework is an ORM (Object Relational Mapping) tool. Object Relational Mapping (ORM) is a technique of accessing a relational database; . i.e., whatever has tables and store procedure these things we interact with database in class, method and property of the object format.


1 Answers

I had the same problem and I found that I had not called

 base.OnModelCreating(modelBuilder); 

in my DbModel

like image 79
Aaron Avatar answered Sep 23 '22 10:09

Aaron