Add details for two entities by using 1 insert page

Last post 07-01-2009 2:16 PM by scaify. 3 replies.

Sort Posts:

  • Add details for two entities by using 1 insert page

    06-30-2009, 7:17 PM
    • Member
      2 point Member
    • scaify
    • Member since 05-16-2007, 11:00 AM
    • Posts 21

     Hi sorry for any stupid questions, this is only my second week playing the EF framework.

    I have in my database a Company table and a User table. What I would like to do is be able to enter the company info and then the user info on the same insert page.

    I have created a custom page by copying the insert page into a company folder in my custom page folder. I have added a second details view and entity data source to the page in the init function I have the following code

    DynamicDataManager1.RegisterControl(DetailsView1)
            DynamicDataManager1.RegisterControl(DetailsView2)
            table = DetailsDataSource.GetTable

            DetailsDataSource2.ContextTypeName = DetailsDataSource.ContextTypeName
            DetailsDataSource2.EntitySetName = "Users"
            DetailsView1.RowsGenerator = New HideColumnFieldsManager(table, PageTemplate.Insert)

            DetailsView2.RowsGenerator = New HideColumnFieldsManager(DetailsDataSource2.GetTable, PageTemplate.Insert)

    In the design view I have set the AutoGenerateInsertButton to false for both detailsview

    When I run the project it shows the two forms and validation works as expected. So I added a button and inside of the button I have the following code

    Dim cont As New mycityisModel.mycityisEntities
    cont.SaveChanges()

    However in the saving changes function osm.GetObjectStateEntries(Data.EntityState.Added) or any other enum returns zero entries. 

    Obviously I am doing something wrong or misunderstanding something. I don't want to create a page with the fields added manually then in the button click event create the two entities and populate them from the fields then calling savechanges as this seems unnecessary and there is probably a way to do it with dynamicdata. Is what I am asking doable. I have till the end of the week to get comfortable with EF or otherwise I have to go back to creating all the classes and input forms myself :( EF is definitely the way forward :)

     

    Any pointers are gratefully received

  • Re: Add details for two entities by using 1 insert page

    07-01-2009, 5:10 AM
    • Contributor
      2,189 point Contributor
    • davidfowl
    • Member since 08-17-2008, 9:50 PM
    • Redmond
    • Posts 449
    • Moderator

    Do you just want to have both details views do the right thing when you click on some other insert button? If so, then you can call DetailsView.InsertItem to fire the inserts on both.

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.insertitem.aspx

    David Fowler
    SDE, ASP.NET Team, Microsoft
  • Re: Add details for two entities by using 1 insert page

    07-01-2009, 7:21 AM
    • Member
      2 point Member
    • scaify
    • Member since 05-16-2007, 11:00 AM
    • Posts 21

     Thanks David

    My only question is how do I tie the 2nd details view to the 1st. The user should be inserted with the companyid of the record inserted from the 1st detailsview.

     in the click event I have 

    DetailsView1.InsertItem(True)
    DetailsView2.InsertItem(True)

    But in the savingchanges event there is only the one entity each time it is called.

     

    thanks in advance for the help

  • Re: Add details for two entities by using 1 insert page

    07-01-2009, 2:16 PM
    • Member
      2 point Member
    • scaify
    • Member since 05-16-2007, 11:00 AM
    • Posts 21

     I have figured this one out but it's probably not the most graceful solution.

    I have created a private variable for a company and a user object at the top of the page

    In my button click event I have the following

    DetailsView2.InsertItem(True)
    DetailsView1.InsertItem(True)

    Dim cont As New mycityisModel.mycityisEntities

    c.Users.Add(u)
    cont.AddToCompanys(c)
    cont.SaveChanges()

    in the detailsdatasource iteminserting event I have the following code

    c = CType(e.Entity, mycityisModel.Company)
    e.Context.Detach(c)
    e.Cancel = True

    It will work for now until I found a more graceful solution

Page 1 of 1 (4 items)