Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?

My error:

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

My 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);
        }
    }
}
like image 683
Bob Avatar asked Oct 18 '13 18:10

Bob


People also ask

What is fluent API in Entity Framework?

Fluent API is an advanced way of specifying model configuration that covers everything that data annotations can do in addition to some more advanced configuration not possible with data annotations.

How do you configure a primary key for an entity in Entity Framework fluent API?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.

What is mapping in Entity Framework?

It is a tool to access the database. More accurately, it's classified as an Object/Relational Mapper (ORM) which means it maps data in a relational database into objects of our applications. Entity Framework. It is a tool to access the database.


2 Answers

The errors occur because the derived identity tables have mapping in the derived context. This needs to be called inside the new OnModelCreating override function. To do this simply add base.OnModelCreating(modelBuilder) to your method as seen below.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{   
    base.OnModelCreating(modelBuilder); // <-- This is the important part!
    modelBuilder.Configurations.Add(new EngineeringDesignMap());
    modelBuilder.Configurations.Add(new EngineeringProjectMap());
}
like image 156
Casey Sebben Avatar answered Oct 12 '22 17:10

Casey Sebben


It looks like you are missing a call to base.OnModelCreating in your DbContext.

like image 21
Hao Kung Avatar answered Oct 12 '22 15:10

Hao Kung