views:

1188

answers:

3

Normally when you update an object in linq2sql you get the object from a datacontext and use the same datacontext to save the object, right?

What's the best way to update a object that hasn't been retreived by that datacontext that you use to perform the save operation, i.e. I'm using flourinefx to pass data between flex and asp.net and when object return from the client to be saved I don't know how to save the object?

   public static void Save(Client client)
    {
        CompanyDataContext db = new CompanyDataContext();
        Validate(client);
        if(client.Id.Equals(Guid.Empty))
        {
            //Create (right?):
            client.Id = Guid.NewGuid();
            db.Clients.InsertOnSubmit(client);
            db.SubmitChanges();
        }
        else
        {
            //Update:
            OffertaDataContext db = new OffertaDataContext();
            db.Clients.????

        }
    }

Update: different approaches to use Attach doens't work in this case. So I guess a reflection based approach is required.

+3  A: 

I think you have 2 options here:

1) Attach the object to the DataContext on which you will do your save 2) Using the primary key on your object, grab an instance that is attached to your context (e.g. do a FirstOrDefault()), and then copy the data over from the modified object to the object that has a context (reflection might be useful here).

Rick Strahl has a very good blog article on attaching entities to a context at http://www.west-wind.com/weblog/posts/134095.aspx, particularly in regards to some of the problems you might encounter.

Matt
+4  A: 

To update an existing but disconnected object, you need to "attach" it do the data context. This will re-use the existing primary key etc. You can control how to handle changes- i.e. treat as dirty, or treat as clean and track future changes, etc.

The Attach method is on the table - i.e.

ctx.Customers.Attach(customer); // optional bool to treat as modified
Marc Gravell
+1, Also: The 'optional bool to treat as modified' only works if there is a TimeStamp field or you specify not to check for updates in the DBML. I use another solution here: http://www.singingeels.com/Blogs/Nullable/2009/01/09/How_to_Make_LINQ_to_SQL_Check_for_Changes_After_Attach_or_AttachAll.aspx
Timothy Khouri
A: 

Hello. I am hoping you can help. I am developing a tiered website using Linq to Sql. I created a new class(or object) in DBML designer called memberState. This object is not an actual table in the database. I have this method in my middle layer:

public override IEnumerable(memberState) GetMembersByState(string @state)
{
using (BulletinWizardDataContext context = DataContext)
{
IEnumerable(memberState) mems = (from m in context.Members
join ma in context.MemberAddresses
on m.UserId equals ma.UserId
join s in context.States
on ma.StateId equals s.StateId
where s.StateName == @state
select new memberState
{
userId = m.UserID,
firstName = m.FirstName,
middleInitial = m.MiddleInitial,
lastName = m.LastName,
createDate = m.CreateDate,
modifyDate = m.ModifyDate
}).ToArray(memberState)();
return mems;
}
}

The tables in my joins (Members, States, and MemberAddresses are actual tables in my Database). I created the object memberStates so I could use it in the query above (notice the Select New memberState. When the data is updated on the web page how do I persist the changes back to the Member Table? My Member Table consists of the following columns: UserId, FirstName, MiddleInitial, LastName, CreateDate, ModifyDate. I am not sure how save the changes back to the database.

Thanks,

Why not create a question of it's own here on SO? You'll get a lot better answers that way! Anyway... when persisting you data you need to fetch the Member and use the changed values from your memberstate instance and update that.
Niels Bosma