locked
How to set Foreign Keys EF4 in WPF? RRS feed

  • Question

  • Hi am a litle desperated. How you can help me to understand the using of Foreign Keys in EF4.

     

    I Have a DB with 3 Tables

    TBL_HEAD:

    ID (PK)

    YEAR int

    GRID_ID int

    HEAD_NUMBER int

     

    TBL_POINT:

    ID (PK)

    YEAR int

    GRID_ID int

    HEAD_NUMBER int

    POINT_NUMBER int

     

     

    TBL_PLANTS:

    ID (PK)

    YEAR int

    GRID_ID int

    HEAD_NUMBER int

    POINT_NUMBER int

    PLANT_NUMBER

     

    These are the Tables. The Associations:

    TABLE_HEAD ---One To Many---  TBL_POINT ---One To Many--- TBL_PLANT

     

    But when I want to set the mapping doesn´t work because I cant´t associate to the foreign key columns?!

    To Create one Set of Data :

    private void Window_Loaded(object sender, RoutedEventArgs e)
    		{
    			TBL_HEAD head = new TBL_HEAD();
    			head.YEAR = 2011;
    			head.GRID_ID = 12345;
    			head.HEAD_NUMBER = ModelContext.TBL_HEAD.Where(h => h.GRID_ID == head.GRID_ID).Count() + 1;
    
    			for (int i = 1; i < 6; i++)
    			{
    				TBL_POINT p = new TBL_POINT();
    				p.GRID_ID = head.GRID_ID;
    				p.YEAR = head.YEAR;
    				p.HEAD_NUMBER = head.HEAD_NUMBER;
    				p.POINT_NUMBER = i;
    				head.TBL_POINT.Add(p);
    				for (int ii = 0; ii < 16; ii++)
    				{
    					TBL_PLANTS1 plant = new TBL_PLANTS1();
    					plant.GRID_ID = head.GRID_ID;
    					plant.YEAR = head.YEAR;
    					plant.HEAD_NUMBER = head.HEAD_NUMBER;
    					plant.POINT_NUMBER = p.POINT_NUMBER;
    					plant.PLANT_NUMBER = ii;
    
    				}
    			}
    


    Do you have any Ideas how to fix it ?

    best regards  and thanks !

     

    Wednesday, July 20, 2011 4:27 PM

Answers