c# - Possible to automatically add elements to collection? -


i new @ entity framework (and stack overflow - first question!):

right got following entities in db:

blog:

[key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int id { get; set; }  public virtual ilist<post> posts { get; set; } 

post:

[key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int id { get; set; }  public int blogid { get; set; }  [required] [foreignkey("blogid")] public virtual blog blog { get; set; } 

currently whenever add post manually add collection of corresponding blog. wonder if it's possible automatically add them collection add posts referencing corresponding blog?

you use entity class create entity rather new, ie:

posts.add(post.create()); 

instead of

posts.add(new blog()); 

but if must pass arguments, need do

post p = post.create(); p.blogid = 1234; p.somestring = "test"; p.somedate = datetme.now;  posts.add(p); 

and always, call db.savechanges();


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -