New Posts New Posts RSS Feed: why would Importing an entity fail?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

why would Importing an entity fail?

 Post Reply Post Reply
Author
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Topic: why would Importing an entity fail?
    Posted: 27-Apr-2014 at 12:30am
I have the following situation. the uses clicks an add button on a form. I create the entity for the add button in question lets say "Memo"

var memo = Repository.CreateMemo(member);

then I call my dialog box to display the memo

MemoEditForm form = new MemoEditForm(memo);
DialogResult result = form.ShowDialog()

in the constructor for the MemoEditForm. I have the following.

InitializeComponent();
Repository = UnionRepository.CreateRepository();
Repository.ClearAllMemberMemos();
currentMemo = Repository.ImportMemberMemo(memo);
editMode = memo.EntityAspect.EntityState == EntityState.Added ? EditMode.Add : EditMode.Edit;
MemberMemoBS.DataSource = currentMemo;
currentMember = currentMemo.Member;
LoadData();

basically I create a new repository/entity manager. I make sure the entity manager is clear. I import the passed over newly created memo object and import it into the new entity manager. if the user enters all the fields in and then clicks "Save" I close the dialog box and refresh a grid with the new memo. If they click cancel, I remove the entity from the first cache.

This works for the first memo. I add a memo (I click cancel). I click add again and the import this time returns a null for currentMemo.

so my question is why would the import fail. this is my import method

     public MemberMemo ImportMemberMemo(MemberMemo memo)
        {
            if (memo == null || memo.EntityAspect.IsNullOrPendingEntity)
            {
               throw new ArgumentException("Imported memo is null or nullo");
            }
            // Don't bother if already attached to repository's manager
            if (memo.EntityAspect.EntityManager == Manager) return memo;

            ImportEntities(new Object[] { memo });
            return (MemberMemo)Manager.FindEntity(memo.EntityAspect.EntityKey);
        }

    private void ImportEntities(IEnumerable entities)
        {
            EnsureEntitiesAreUnmodified(entities);
            Manager.ImportEntities(entities, MergeStrategy.OverwriteChanges);
        }
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 28-Apr-2014 at 11:53am
Hi BillG,

Are you using an integer data type with Database Identity generation for your Memo's primary key?
Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 28-Apr-2014 at 12:21pm
yes. It is an identity column.

Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 28-Apr-2014 at 2:43pm
As you've already noticed, DevForce will generate a temporary key for entity with a PK identity column.

Entities with temporary key and EntityState of Added are considered new entities by the EntityManager. What this means is that when you import entities with the same id and EntityState of Added, the later import doesn't overwrite the earlier import. But rather, the EntityManager will see the later entity as a new entity and will increment the id. So what you have are 2 entities with different ids.

I suspect that this is what's happening here. The fix is to set the EntityState to Unchanged before import and set them back to Added before persisting them to the db.
Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 28-Apr-2014 at 3:17pm
What i dont understand is that when i pop up the dialog box for Memo form i am creating a new repository before importing the passed in memo object. Why would the previous one still be there?

going back to my example. on the list form, the user clicks Add, i create the memo and pass it to the dialog box form. in the dialog box form, i create a new repository/entity manager, import the passed in memo to the newly created entity manager. if i create a new repository/entity manager, clear it of all memos, and then do the import, should not that one be the only memo in the cache?

Bill



Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 28-Apr-2014 at 7:14pm
Sorry, I missed that you are creating a new EntityManager upon each add. 

If that's the case, the next thing to debug is, make sure the Import succeeds, and right after the import, search the EM's cache by using the EntityManager.FindEntities method. This should only return 1 entity of type Memo and that its EntityKey is what you'd expect.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down