locked
Issue With Foreign Keys on Insert.aspx RRS feed

  • Question

  • User-1521052036 posted

    I have an entity with a child entity.  For discussion purposes the Parent is "Bank" and the child is "Account".   The relationship is 0..1 Banks to Many Accounts.   They're displayed in a custom FieldTemplate that displays the information in a grid.

    Inside the Bank.cs class, the account field is described as thus:

     [Display(Order = 4, Name = "Accounts", GroupName = "Details")]
     [UIHint("OneToManyGrid")]
     [ShowColumns("Account_Name", "ID")]
     public EntityCollection<Account> Accounts { get; set; }

    Everything works great on the display, the Editing, etc.   When I go to Insert a Bank, Account disappears.    This make sense, as there's no Bank yet, to insert Accounts against, but...

    I need to show...  something.   An empty grid with text inside saying "Please hit Insert, then Edit to add Accounts."   some kind of text to indicate that Accounts are part of the Bank entity, and need to be addressed by the user.

    I would normally look to address this via the Field Template, by doing something when in Insert Mode.  But, I believe, the property isn't being scaffolded, so the FieldTemplate is never invoked.

    Is there a way that I can insert my text in place of an association on the Insert page?

    Tuesday, November 24, 2015 9:38 PM

All replies

  • User-271186128 posted

    Hi FieldTemplate,

    Welcome to asp.net forum.

    Is there a way that I can insert my text in place of an association on the Insert page?

    From your description, it seems that you want to insert the Bank and Account in the insert page. If that is the case, I suggest you could add some TextBox to fill the Bank's fields, and use a GridView to insert the Account. After adding an Account, you could use session to save the inserted records. Then, when you click the Save button, you could get the Bank's information from the TextBox control and get the Account information from the session.

    Then, you could refer to the following code to insert new records with one to many relationship:

    public void InsertData()
            {
                Product pro = new Product();
                pro.ProductId = 1;
                pro.Name = "name1";
                Category cat1 = new Category();
                Category cat2 = new Category();
                Category cat3 = new Category();
                cat1.CategoryID = "cat01";
                cat2.CategoryID = "cat02";
                cat3.CategoryID = "cat03";
                pro.Category.Add(cat1);
                pro.Category.Add(cat2);
                pro.Category.Add(cat3);
                using (DemonEntity ctx = new DemonEntity())
                {
                    ctx.InsertIntoProducts(pro);
                    ctx.SaveChanges();
                }
            }

    More details, please refer to this link: http://forums.asp.net/t/1935219.aspx?+Entity+Framework+Insert+new+record+with+relationship+one+to+many

    Best regards,
    Dillion

    Wednesday, November 25, 2015 2:19 AM
  • User-1521052036 posted

    Ah, Thank you Dillion, but it's not exactly what I'm looking for.

    If I can get this working, eventually I'll get around to allowing them to "insert" accounts, then actually save them when hitting the "Insert" button for the bank.   What I would like to do is just give them a link to "Insert New Account", which would save the record that they're on, and then go to the Insert.aspx page for accounts.

    The main problem is that none of the foreign keys (accounts) show up on the Insert.aspx page for the bank, or any other entity with a one to many relationship.   Many to One relationships show, and many to many do as well.

    I could build a custom page for this to work, but then I would have to build a custom page for every entity in the model, and it would defeat the purpose of Dynamic Data.

    Thanks for your response though, I appreciate the time you took for that.

    Monday, November 30, 2015 2:24 PM
  • User-330204900 posted

    Hi prylott1234, this is the chicken and egg issue, What you need is a Insert FieldTemplate like you have an Edit and read-only try adding a variant called Name_Insert.ascx. I get around this issue with custom pages myself as it is usually only my main entity i.e. Orders->OrderLines kind of relationship. So what I do is have a custom insert page that is actually a copy of the Edit page and I insert an empty Order then edit that new Order which gives me the Order Lines grid. But I think this is also not exactly what you want?

    Tuesday, December 1, 2015 10:19 AM