Add Entitycollection to new Entity

Posts   
 
    
G.I.
User
Posts: 172
Joined: 09-Jun-2005
# Posted on: 20-Aug-2007 15:30:37   

Hi,

I have been searching on the forum, and although it is probably there, I couldn't find the solution to this problem:

I have a page where i create a project. On this project I am adding related data. For example: Budget:

This Buget contains ProjectID, Year, Amount.

So the Project has a collection called Budgets. Now I create a new project and would like to populate the Budgets EntityCollection.


ProjectEntity currentProject = new ProjectEntity();

BudgetEntity budget = new BudgetEntity();
budget.Year = Convert.ToInt32(ddlYear.SelectedValue);
budget.Amount = Convert.ToInt32(txtAmount.Text);

currentProject.Budgets.Add(budget);

Problem is that currentProject.Budgets is null ... and I can't do, because it's readonly:

currentProject.Budgets = new EntityCollectino<Budgetentity>();

But I don't want to save the project and later add the budgets ... this should be done in one transaction ...

Can someone helpe me with this?

Tnx in advance!

  • G.I.
goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 20-Aug-2007 18:27:30   

you can skip this linesmile :

currentProject.Budgets = new EntityCollection<BudgetEntity>();

because the Budgets Collection is not null, so this block should work neatly:

ProjectEntity currentProject = new ProjectEntity();

BudgetEntity budget = new BudgetEntity(); budget.Year = Convert.ToInt32(ddlYear.SelectedValue); budget.Amount = Convert.ToInt32(txtAmount.Text);

currentProject.Budgets.Add(budget);

G.I.
User
Posts: 172
Joined: 09-Jun-2005
# Posted on: 21-Aug-2007 09:29:00   

tnx, found also that project entity was not correctly initiated all the time ... postback stuff ... now it works simple_smile