locked
C# DBContext Entity Framework dynamic DBSet property RRS feed

  • Question

  • Hi

      I am new to Entity Framework and I have created class derived from DbContext with following example.

    public partial class StudentdbContext : DbContext{
      public StudentdbContext()
                : base("Name=StudentdbContext"){
            }
         
            public DbSet<student> students { get; set; }      
                  
            protected override void OnModelCreating(DbModelBuilder modelBuilder){          
                modelBuilder.Configurations.Add(new studentMap());        
            }      
    }

    What I am more interested to load "studentinfo" entity data using this context without adding new DBSet entity property "public DBSet<studentinfo> studentsInfo {get;set;}" at DESIGN time.

    Is it possible ? Basically I want DbContext class to handle different entities runtime.

    thanks


    • Edited by drew_p Monday, July 1, 2013 1:40 PM corrected
    • Moved by CoolDadTx Monday, July 1, 2013 2:10 PM EF related
    Monday, July 1, 2013 1:33 PM

Answers

  • Use the generic Set<T>() method:

    IQueryable<studentinfo> studentInfo = studentdbContext.Set<T>();

    • Marked as answer by drew_p Friday, July 5, 2013 6:32 PM
    Monday, July 1, 2013 1:38 PM
  • Hi Drew,

    I don't have a good idea to add the map configuration at runtime. I'm afraid you need to add the studentInfoMap configuration class in the OnModelCreating() method. Otherwise the Entity Framework doesn't know to map the entities classes to the database.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by drew_p Friday, July 5, 2013 6:32 PM
    Wednesday, July 3, 2013 10:34 PM

All replies

  • Use the generic Set<T>() method:

    IQueryable<studentinfo> studentInfo = studentdbContext.Set<T>();

    • Marked as answer by drew_p Friday, July 5, 2013 6:32 PM
    Monday, July 1, 2013 1:38 PM
  • thanks for quick reply

    It works only if StudentContext class OnModelCreating() method has "modelBuilder.Configuration.Add(new studentInfoMa())" otherwise it loads all the records (in this case 360 Records for me) but they all are null. 

    Is there any was to define "studentInfoMap" for "studentInfo" entity at runtime ?

    thanks

     

    Monday, July 1, 2013 2:08 PM
  • Is anyone has answer for above question ?

    thanks


    Drew

    Tuesday, July 2, 2013 1:08 PM
  • Hi Drew,

    I don't have a good idea to add the map configuration at runtime. I'm afraid you need to add the studentInfoMap configuration class in the OnModelCreating() method. Otherwise the Entity Framework doesn't know to map the entities classes to the database.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by drew_p Friday, July 5, 2013 6:32 PM
    Wednesday, July 3, 2013 10:34 PM
  • I guess it would be nice if entity framework has supported to assign "EntityMap" class file runtime too (may be OnModelUpdate()??). because we can assign Entity class using "DBContext.Set<T>()" runtime , we can't assign EntityMap class file for it runtime.

    But thanks for clarification on it.


    Drew

    Friday, July 5, 2013 11:44 AM
  • Sunday, November 3, 2013 7:05 PM