allowing a user to define a table at runtime using entity framework

.net c# entity-framework entity-framework-6

Question

is it possible to dynamically build entities in entity framework 6 using code first?

is it also possible to add properties to those entities at runtime?

Can those entities reference each other using foreign keys?

If someone could point me in the right direction if this is possible, i would really appreicate it.

EDIT

i have changed the title to reflect more of what i would like, so u can see why i asked the questions i asked.

Basically i would like to create a system where a user can define the objects(tables) they want, with the properties on that object (columns in table) as well as the references to each object (relationships between tables).

Now each object would have an Id property/column. i was thinking of atoring these definitions in an eav like table structure but i have other ibjects that are defined at design time and i like to use linq queries on these objects. I would also like to be able to build linq queries on these objects as users will want to report on this data. Building the linq queries should be fine as could use dynamic linq for this (Maybe)?

Currently to create such a system i have created a table that has many text fields, many number fields, many relationship fields and users can use which ones they want. But this is just 1 table and i think this is going to bite me in the bottom in the end, thus why i would like to take it to the next level and build separate tables for each object.

if anyone knows of a better way or maybe experienced something similar, glad to hear opinions.

1
1
4/26/2015 7:29:21 PM

Popular Answer

You can create an EF model dynamically using reflection:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");

    foreach (Type type in ...)
    {
        entityMethod.MakeGenericMethod(type)
            .Invoke(modelBuilder, new object[] { });
    }
    base.OnModelCreating(modelBuilder);
}
1
4/25/2015 10:56:20 PM


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