c# - How to add DbSet<EntityName> to a context that is generated dynamically with Codedom? -


i generating entities dynamically using codedom. don't have hardcoded context class part of solution. is, generating contexts @ run time using codedom. doing every generated entity has own context. running problem writing codedom code context class. part of context, need write in dbset property generated entity can part of model context. more specifically, need following line in generated context:

public dbset<entityname> entitynames { get; set; } 

where entityname name of entity class type have created, , entitynames name 's' on end. example, might create employee entity, generate context with:

public dbset<employee> employees { get; set; } 

to writing method returns codememberproperty. knows write employee/employees because pass in name of entity. that's fine. method looks far:

public static codememberproperty hardcodedbset(string contextname) {     string entityname = contextname.substring(0, contextname.length - 7);     codememberproperty prop = new codememberproperty();     prop.attributes = memberattributes.public | memberattributes.final;     prop.name = entityname + "s";     prop.type = new codetypereference(typeof(dbset<>));     return prop; } 

the reason line:

string entityname = contextname.substring(0, contextname.length - 7); 

is make name of generated context corresponding entity name + "context", in line removing "context" entityname. anyway, that's not relevant. line that's giving me trouble type one:

prop.type = new codetypereference(typeof(dbset<>)); 

this gives me:

dbset<> 

but need:

dbset<entityname> 

if try write inside angle brackets error saying can't resolve symbol. example write:

prob.type = new codetypereference(typeof(dbset<entitytype>)); 

and make

type entitytype 

a parameter of method, doesn't that.

does know way around this? thought lot simpler code this, codedom isn't cooperative thought sometimes...

what you're asking here, if go heart of matter, how use reflection type of closed generic type. doesn't have codedom or entityframework specifically.

as mentioned, using typeof(dbset<>) (or list<>, or anything) give "open generic type", meaning 1 specific generic types based on. here, can use method makegenerictype create specific closed generic type:

var opentype = typeof(dbset<>); var closedtype = opentype.makegenerictype(entitytype); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -