POCO Data updates

Sep 13, 2010 at 8:20 PM

Hi,

 

Great article!

I have done some testing with E4 based on your / this project: Lazy loading, getting data based on a complete graph back, but I do have some questions:

 

ExpenseDataAccess:

        public List<Expense> SelectActive()

        {

            using (ExpenseDataContext ctx = new ExpenseDataContext())

            {

             return ctx.Expenses

              .Include("ExpenseReviews")

              .Include("ExpenseLogs")

              .Where(e => e.IsCompleted == false)

              .ToList();

            }

        }

 

ExpenseComponent:

 

   public Expense UpdateExpense(Expense expense)

{…}

 

TestCode:

   var bc = new ExpenseComponent();

   var expenses = bc.ListActiveExpenses();

   if (expenses.Any())

   {

    var expense = expenses.FirstOrDefault();

    if (expense.ExpenseReviews.Any())

    {

     expense.ExpenseReviews.FirstOrDefault().Remarks = "test " + DateTime.Now.ToLongDateString();

     bc.UpdateExpense(expense);

    }

   }

 

Test code in ExpenseDataAccess.Update:

                ctx.DetectChanges();

                var changed = ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);

                var cnt = changed.Count();

 

Why isn’t the ExpenseReview record updated in the database and if I test the code update in the ExpenseDataAccess I always get a ‘changed count’ of 1 even with NO updates.

All help welcome ;-)

 

TIA,

Jean-Pierre.

Coordinator
Sep 14, 2010 at 12:40 PM

Hi!

I am not really a pro in EF but could it be because I have the following code in the constructors of the ExpenseDataContext class?

        public ExpenseDataContext()
            : base("name=ExpenseEntities""ExpenseEntities")  
        {
            this.ContextOptions.LazyLoadingEnabled = false;
            this.ContextOptions.ProxyCreationEnabled = false;
        }

 

Best Regards,

Serena Yeoh

Sep 14, 2010 at 5:40 PM
Edited Sep 15, 2010 at 9:19 AM

Hi Serena,

I have already disabled LazyLoading ;-) But I have problems with the updates in the child records. Your project is (one of) the best layered examples with EF, but what layer is responsible for the update of the child records. Should we handle the child updates in ExpenseComponent or in the DAC?

Do you have experience with the CTP 'Self Tracking Enities' http://blogs.msdn.com/b/adonet/archive/2009/06/22/feature-ctp-walkthrough-self-tracking-entities-for-the-entity-framework.aspx ? Maybe that's the solution?!

Thanx,
Greetings Jean-Pierre.

Coordinator
Sep 16, 2010 at 3:47 AM

Hi Jean,

I think you have to enable lazy loading. The data layer is responsible for all create, read, update and delete (CRUD) operations. The sample does not contain any self-tracking code. You many need to check with the ADO.NET EF documentation on how to create self-tracking entities with POCO.

Sorry for my limited knowledge in ADO.NET EF. I may consider looking at it when I have the time.

Best Regards,
Serena 

Feb 23, 2011 at 8:52 PM
SjanPjer wrote:

Your project is (one of) the best layered examples with EF

Hi,

I'm looking for examples just like that and this one is the only decent one I have found so far. I am looking to expand my knowledge and understanding in order to spice my dev life up a bit ;) I've been stuck on the same projects and technologies for years...

Would you have other examples available for download?

Thanks!

Seb :)