Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I map tables using fluent API in ASP.NET MVC 5, Entity Framework 6?
    text
    copied!<p>I am trying to create a one to one relationship using C# in Entity Framework 6 using ASP.NET MVC 5 with built-in user authentication.</p> <p>I am able to make tables and connections with the defaults that Entity Framework creates. But when I try to use fluent API.... more specifically when I use on model creating even empty my database migration using the package manager console will fails. How can I map my one to one relationship?</p> <p>My error:</p> <pre><code>//error //my.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. //Define the key for this EntityType. //my.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. //Define the key for this EntityType. //IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type //'IdentityUserLogin' that has no keys defined. //IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type //'IdentityUserRole' that has no keys defined. </code></pre> <p>My Code:</p> <pre><code>namespace my.Models { public class ApplicationUser : IdentityUser { } public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt; { public ApplicationDbContext() : base("DefaultConnection") { } public DbSet&lt;EngineeringProject&gt; EngineeringProjects { get; set; } public DbSet&lt;EngineeringProject&gt; EngineeringDesigns { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new EngineeringDesignMap()); modelBuilder.Configurations.Add(new EngineeringProjectMap()); } } } namespace my.Models.Mapping { public class EngineeringProjectMap : EntityTypeConfiguration&lt;EngineeringProject&gt; { public EngineeringProjectMap() { this.HasRequired(t =&gt; t.EngineeringPd) .WithOptional(t =&gt; t.EngineeringProject); this.HasRequired(t =&gt; t.EngineeringProjectCategory) .WithMany(t =&gt; t.EngineeringProjects) .HasForeignKey(d =&gt; d.CategoryId); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload