How can I map tables using fluent API in ASP.NET MVC 5, Entity Framework 6?

asp.net-identity asp.net-mvc-5 c# entity-framework entity-framework-6

Question

With the help of ASP.NET MVC 5 and built-in user authentication, I'm attempting to establish a one-to-one relationship using C# and Entity Framework 6.

I can construct tables and connections using Entity Framework's default settings. But when I try to utilize the fluent API—more precisely, when I use the package management console to migrate a database that is even empty—it will fail. How can my one-to-one relationship be mapped?

My blunder

//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.

I'm Code:

namespace my.Models
{

    public class ApplicationUser : IdentityUser
    {
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<EngineeringProject> EngineeringProjects { get; set; }

        public DbSet<EngineeringProject> 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<EngineeringProject>
    {
        public EngineeringProjectMap()
        {
            this.HasRequired(t => t.EngineeringPd)
                .WithOptional(t => t.EngineeringProject);
            this.HasRequired(t => t.EngineeringProjectCategory)
                .WithMany(t => t.EngineeringProjects)
                .HasForeignKey(d => d.CategoryId);
        }
    }
}
1
11
3/10/2017 1:01:56 AM

Accepted Answer

The mapping in the derived context of the derived identity tables causes the mistakes. The new OnModelCreating override function needs to call this. Simply add base.OnModelCreating(modelBuilder) as shown below to your method to accomplish this.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{   
    base.OnModelCreating(modelBuilder); // <-- This is the important part!
    modelBuilder.Configurations.Add(new EngineeringDesignMap());
    modelBuilder.Configurations.Add(new EngineeringProjectMap());
}
19
2/9/2014 10:12:04 AM

Popular Answer

You appear to have missed a call tobase.OnModelCreating within the DbContext.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow