b4st

Hi all,

for a project I have, my team decided to try working with Entity Framework and use LINQ to query the entities. To make my question more visible, I've also embedded a simplified code of
our project which you can see below. We're using 3-Layer architecure (Presentation, Business, Data). The Main class below represented the Presentation Layer, and both getParticipant(...) and saveParticipant(...) methods are to be placed in the data layer.

What I want to do here is :

1. Create a participant object in the presentation layer (Main class), using incomplete information (here in the sample the Participant_ID).
2. Get the rest of the information using a LINQ query in getParticipant(...) method, and save it to the created object.
3. Change the information of the entity and save it using saveParticipant(...) method

As you can see, the context to the entity framework is only available in get and saveParticipant(...) methods of the data layer, this is intentional for security reasons.

The problem arise when I call the saveParticipant(participant) method, practically forwarding the participant object to the "data layer". I don't know which method from the entity framework to call to incorporate the changes to the entities. I tried using "context.Attach(iParticipant);" to attach the input to the context, but then we get this error "Message="Entity can not be attached to multiple change trackers." eventhough I've detached them 1st in the getParticipant(...) method.

I tried just using the context.AddToParticipant(iParticipant) method, but it failed by giving out "The entity cannot be added to the ObjectStateManager because it already has an EntityKey attached to it.", which is understandable

I think the participant object have all the information needed for the entity framework to work with. any suggestion is very appreciated :-)

Thanks in Advance,

b4st

--------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
class Program
{
static void Main(string[] args)
{
EntityFrameworkModel.Participant participant = new EntityFrameworkModel.Participant();
participant.Participant_Id = 1;

participant = getParticipant(participant);

participant.First_Name = "Pete";
saveParticipant(participant);

}

public static EntityFrameworkModel.Participant getParticipant(EntityFrameworkModel.Participant iParticipant)
{
using (EntityFrameworkModel.Entities context = new EntityFrameworkModel.Entities())
{
var firstParticipant = (from fp in context.Participant
where fp.Participant_Id == iParticipant.Participant_Id
select fp);
context.Detach(firstParticipant.First<EntityFrameworkModel.Participant>());
return firstParticipant.First<EntityFrameworkModel.Participant>();
}
}

public static void saveParticipant(EntityFrameworkModel.Participant iParticipant)
{
using (EntityFrameworkModel.Entities context = new EntityFrameworkModel.Entities())
{
//...
context.SaveChanges();
}
}
}
}

----------------------------------------