Update with Entity Framework

Update with Entity Framework

Old forum URL: forums.lhotka.net/forums/t/9570.aspx


tsvideo posted on Friday, September 24, 2010

Hello !
I'am New to CSLA. We want to use it in a new application, and
so i'm testing it by writing a littel sample application.

We want to use Entity Framework as Data Layer.

My Question is:

The following Code (taken from Rolodex Sample and CSLAExtension generator)
does not work:

[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Update()
{
  using (var contextManager = Csla.Data.ObjectContextManager<MyCSLAWPFAppEntities>.GetManager(Database.Name))

  {
    DataLayer.Kunde data = new DataLayer.Kunde();
   
    data.ID = ReadProperty(IDProperty);
    data.EntityKey = new System.Data.EntityKey("MyCSLAWPFAppEntities.Kunden", "ID", data.ID);
   
    contextManager.ObjectContext.Attach(data);
    //***********************************************
    //Error from ObjectStateManager: Duplicate Entity
    //Translated from German: 'an object with same Key ist already in ObjectStateManager'
    //***********************************************

    WritePropertiesToData(data);

    contextManager.ObjectContext.SaveChanges();
  }
}

The problem ist, by Attaching the Object to the Context, the ObjectStateManger
throw the exception, that he already have an object with the Entity Key.
This is right, because i'm only Updateting the Object. But Why does it
Work in the Rolodex sample, what do they other ?
(Another Question: is it possible to get an running Rolodex WPF sample ?)

Now I'am using this Working Code, but is it right ?

[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Update()
{
  using (var contextManager = Csla.Data.ObjectContextManager<MyCSLAWPFAppEntities>.GetManager(Database.Name))

  {
    Guid id = ReadProperty(IDProperty);
    var data = (from kunde in contextManager.ObjectContext.Kunden
                where kunde.ID == id
                select kunde).Single();

    WritePropertiesToData(data);

    contextManager.ObjectContext.SaveChanges();
  }
}

Please can you help me?

sergeyb replied on Friday, September 24, 2010

It is likely that you have the data from fetching the object in the first place.  I.e., the context has not been disposed between you fetching and updating the entry.

tsvideo replied on Monday, September 27, 2010

Not Only the Problem with the ObjectManger,
I also have a problem with 'EnlistTransaction', the Error is 'MSDTC' ist not startet.
 
After Reading some Posts in this Forum concerning 'MSDTC' i get whats going wrong !
 
All Data Access i'm doing must wrapped into the using clause. I think this is an Error from a Beginner (me)!
 
i.e.:
    public void CreateSomeKunden
    {
      //***** Solution -> the next using solves my problem with 'MSDTC' and ObjectStateManager
      using (var context = ObjectContextManager<MyCSLAWPFAppEntities>.GetManager(Database.Name))
      {
        for (int i = 1; i < 11; i++)
        {
          var kunde = Kunde.NewKunde();
          kunde.Kundennr = String.Format("{0:0000000}", i);
          kunde.Suchbegriff = String.Format("Suchbegriff {0}", i);
          kunde.Save();
        }
        context.ObjectContext.SaveChanges();
      }
    }
 
After doing this, the DataPortal_update code with Entity Key Works and Insert/Update also.

Copyright (c) Marimer LLC