none
Web project getting and updating entities from Data access project RRS feed

  • Question

  • My 1st try with Entity framework as datalayer.  I use the code below to get data and can update the local entities.

    How would I save the changes, pass the List<ContractRate> back to the DAL and then...?  In ContractById should i be returning a List<ContractRate> ?

    The book I am reading only does the changes in the same project so i don't really understand this part


            protected void Page_Load(object sender, EventArgs e)
            { 
                if (IsPostBack)
                    return;

                List<ContractRate> contracts  = DAL.Contracts.ContractById(2);
                var c1 = contracts.FirstOrDefault();

                Session["C"] = contracts;
                gv.DataSource = contracts;
                gv.DataBind();
            }

    namespace DAL
    {
        public static class fContracts
        {
            public static List<ContractRate> ContractById(int id)
            {
                using (var context = new FEntities())
                {
                    var f = context.Contracts
                        .Where(c => c.Id == Id)
                        .Select(c => c);

                    return f.ToList<ContractRate>();
                }
            }
        }
    }


    M~

    Thursday, July 12, 2012 5:51 PM

Answers

  • Hi WIJ,

    The code you posted have loaded the contracts list into the memory, then you can do any update about this list, ObjectStateManager will help you to manage the state of the entity, when you call SaveChanges, all the updates will persist to the database.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 16, 2012 5:37 AM
    Moderator

All replies

  • On 7/12/2012 1:51 PM, WIJ wrote:
    > My 1st try with Entity framework as datalayer. I use the code below to
    > get data and can update the local entities.
    >
    > How would I save the changes, pass the List<ContractRate> back to the
    > DAL and then...? In ContractById should i be returning a
    > List<ContractRate> ?
     
    You should be return a contract by id that has children rate objects
    >
    > The book I am reading only does the changes in the same project so i
    > don't really understand this part
    >
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (IsPostBack)
    > return;
    >
    > List<ContractRate> contracts = DAL.Contracts.ContractById(2);
    > var c1 = contracts.FirstOrDefault();
     
    The FirstOrDefault() should have been used ContractById() method and its
    query.
     
    you can do....
     
    var contact = DAL.Contracts.ContractById(2);
    >
    > Session["C"] = contracts;
    > gv.DataSource = contracts;
    > gv.DataBind();
    > }
    >
    > namespace DAL
    > {
    > public static class fContracts
    > {
    > public static List<ContractRate> ContractById(int id)
    > {
    > using (var context = new FEntities())
    > {
    > var f = context.Contracts
    > .Where(c => c.Id == Id)
    > .Select(c => c);
    >
    > return f.ToList<ContractRate>();
    > }
    > }
    > }
    > }
     You should so an "Include('ContractRate')" on the above query to get the
    Contact by its id with all its ContractRate children and just return
    Contact.
     
    The you would just send Contact back with its ContactRate children.
     
    Then you would save the children.
     
     
    Friday, July 13, 2012 12:38 AM
  • A contract rate is just a rate with dates and values, no children no parents.  What I am trying to get to is how can i return the contracts from the query and not use toList?  Or is toList the proper way to do it?  

    In the client someone can filter and select a specific contract and modify.  I then send the contract itself back to be saved.  Is this the proper way or is there a way to make multiple changes to the contracts from List<contract> and then send that back to update all?

    Thanks!

    M~

    Friday, July 13, 2012 1:05 PM
  • Hi WIJ,

    The code you posted have loaded the contracts list into the memory, then you can do any update about this list, ObjectStateManager will help you to manage the state of the entity, when you call SaveChanges, all the updates will persist to the database.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 16, 2012 5:37 AM
    Moderator
  • Hi WIJ,

    Any update about this issue? If you need further help, please feel free to let me know, I will be more than happy to be of assistance.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, July 19, 2012 7:52 AM
    Moderator