locked
Enitity Framework Inheritance Mapping RRS feed

  • Question

  • I got this assignment where I need to map a class which is inheriting from an abstract class and which intern implements multiple interfaces. I mapped using DbContext ,Configurations ,Fluent APIs to SqlServer. Now when I am trying to assign the values using  context.Instructor.Add(instructor); here throws me error....


    • An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll
    • Additional information: The type 'Instructor' has already been configured as an entity type. It cannot be reconfigured as a complex type.

    why is it throwing this error?

    Is there any work around to get the code to work?

    is it  possible to implement the class this way?

    My Interfaces:

    public interface IA
        {
            int Id { get; set; }
        }

    public interface IB : IA
        {
            string Name { get; set; }
        }

    The abstract Class implements these interfaces:

    public abstract class Person :IB, IA
        {
            public int Id { get; set; }

            public string Name { get; set; }

            
        }

     Now the Class is inheriting from this Abstract class and implementing Enum(Roles)


    public class Instructor : Person
        {
            public int InstructorId { get; set; }
            public Roles Roles { get; set; }

        }

         public enum Roles
        {
            principle = 1,
            teacher = 2,
            Admin = 4,
            Supervisor = 8,
         }

    The Configuration:


     public class InstructorConfiguration : EntityTypeConfiguration<Instructor>

        {     


    public  InstructorConfiguration()

            {

                HasKey(p => p.Id);

                Property(p => p.Id).HasColumnOrder(0);

                Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

                Property(a => a.Name)

                    .HasMaxLength(200)

                    .IsRequired();
    this.Map(m => m.ToTable("Person"));

            }

                   }


    The Context

    public class Context :  DbContex

    public Context() : base("name=Context")

    {

    Database.SetInitializer<Context>(null);


    }

    public virtual DbSet<Instructor> Instructors { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)

            {

                modelBuilder.Configurations.Add(

                new InstructorConfigurations());

    }

    }


    in the Console Application I am trying to assign values


    class Program

        {

    static void Main(string[] args)
               
    {

    using ( var context = new Context())

    {

    Instructor instructor=new Instructor()

    {

    Name ="xyz",
    Email="bdsfjh",
    Dob = DateTime.Now,
     };

    context.Instructor.Add(instructor);////throwing an error message.

     context.SaveChanges();

    }

    }

    Tuesday, June 28, 2016 2:26 PM

Answers

  • This worked fine for me with EF 6.1.3

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication11
    {
        public interface IA
        {
            int Id { get; set; }
        }
    
        public interface IB : IA
        {
            string Name { get; set; }
        }
    
        public abstract class Person : IB, IA
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public DateTime Dob { get; set; }
        }
    
    
        public class Instructor : Person
        {
            public int InstructorId { get; set; }
            public Roles Roles { get; set; }
            public string Email { get; set; }
    
        }
    
        public enum Roles
        {
            principle = 1,
            teacher = 2,
            Admin = 4,
            Supervisor = 8,
        }
    
    
        public class InstructorConfiguration : EntityTypeConfiguration<Instructor>
        {
            public InstructorConfiguration()
            {
                HasKey(p => p.Id);
                Property(p => p.Id).HasColumnOrder(0);
                Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
                Property(a => a.Name)
                    .HasMaxLength(200)
                    .IsRequired();
                this.Map(m => m.ToTable("Person"));
            }
        }
    
    
    
        public class Context : DbContext
        {
            public Context() : base("server=.;database=context;integrated security=true")
            {
                Database.SetInitializer<Context>(null);
            }
            public virtual DbSet<Instructor> Instructors { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.Add(
                   new InstructorConfiguration());
            }
    
        }
    
    
        class Program
        {
            static void Main(string[] args)
            {
                using (var context = new Context())
                {
                    context.Database.Create();
                    Instructor instructor = new Instructor()
                    {
                        Name = "xyz",
                        Email = "bdsfjh",
                        Dob = DateTime.Now,
                    };
    
                    context.Instructors.Add(instructor);////throwing an error message.
                    context.SaveChanges();
                }
            }
        }
    }
    

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, June 28, 2016 2:53 PM