Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. In my model, I have an entity called Person and Learner and Teacher inherit from Person. When I attempt to add data for a Learner as thus:

C#
using (var db = new SCSOMLAModelContext())
            {
                var learner = new Learner
                {
                    FirstName = firstName,
                    MiddleName = middleName,
                    LastName = lastName,
                    UserId = 1,
                    RegistrationNumber = registrationNumber
                };

                db.

            }

the context object "db", seem not to contain information about the inheritance types as thus:

C#
public class SCSOMLAModelContext : DbContext
    {
        public SCSOMLAModelContext();

        public virtual DbSet<Person> People { get; set; }
        public virtual DbSet<User> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder);
    }


I am using Model First approach and without this entities (Learner and Teacher), I am unable to add data to them. In my database, I have this tables available as "People_Learner", and "People_Teacher".

Please assist on how to add data to this tables, I am totally stuck. And why are these two entities (Learner and Teacher) not on DbContext? I am using VS2013.
Posted

you should write like this..
C#
using (var db = new SCSOMLAModelContext())
            {
                var learner = new Learner
                {
                    FirstName = firstName,
                    MiddleName = middleName,
                    LastName = lastName,
                    UserId = 1,
                    RegistrationNumber = registrationNumber
                };

                db.Learners.AddObject(learner);
                db.SaveChanges();

            }


this will insert record to the Learner table.
 
Share this answer
 
Comments
Wamuti 25-Nov-13 11:09am    
db.Learners.AddObject(learner);

I agree. This is the next line, but, Learners is not available from DbContext. Thus, when I try the above line of code, "db.Learners" is not accepted.
Er Daljeet Singh 25-Nov-13 23:45pm    
which class you had added/createed on the dbcontext.
The problem is that, as I had said, Model First does not add the derived classes (Learner and Teacher) in DbContext. The changes I made for the DbContext inheriting class is this:

C#
public partial class SCSOMLAModelContext : DbContext
    {
        public SCSOMLAModelContext()
            : base("name=SCSOMLAModelContext")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Learner>().ToTable("User");
            modelBuilder.Entity<Teacher>().ToTable("Person");
        }

        public virtual DbSet<User> Users { get; set; }
        public virtual DbSet<Person> People { get; set; }
        public virtual DbSet<Learner> Learners { get; set; }
        public virtual DbSet<Teacher> Teachers { get; set; }
    }


It works! :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900