LINQtoSQL beta: Add save() method to objects, partial class

LINQtoSQL beta: Add save() method to objects, partial class

Postby yuri@wallie.com » Wed 08 Oct 2008 09:57

Wouldn't it be great to have a Save() method on all the dataobjects?

Explanation:

When i have a collection of dataobjects, i don't have my datacontext anymore, so they are stateless. But i want to be able to save these objects to the database later on.

This could be accomplished by have a Save() method on all objects, that gets the datacontext, checks if the primary key exists in the table (if so, it performs
an update, otherwise it performes a insert).

This currently is possible by writing a manual Save() method in the partial class of each object.

It sure would be nice to have such a method generated by the schema modeller.

Also described here : http://forums.asp.net/p/1207566/2135749.aspx

Is there any change that something like that is going to be implemented ?

Regards,

Yuri.
yuri@wallie.com
 
Posts: 27
Joined: Mon 08 Sep 2008 16:37

Postby Shalex » Wed 08 Oct 2008 16:23

Have you tried standard Devart.Data.Linq.Table.InsertOnSubmit method?
Code: Select all
DataContext.Company c = new DataContext.Company();
db.Companies.InsertOnSubmit(c);
Shalex
Devart Team
 
Posts: 7293
Joined: Thu 14 Aug 2008 12:44

Postby yuri@wallie.com » Mon 13 Oct 2008 10:08

Ok, i think this is not a discussion about your product, but more about linq in general. Buy maybe you want to answer my question because i don't know how to accomplish this (without having to retrieve all objects twice from the database).

Ofcourse i know why it doesnt work, but i have no clue whatsoever how to accomplish this (well i could make the datacontext a public property in each object so i don't lose it, but i don't know if that is the right way)

Also there is no db.update (so i can tell weather it has to update or insert).

This works:

Code: Select all
umbrellaDataContext.umbrellaDataContext db = new umbrellaDataContext.umbrellaDataContext();

Orderr order = db.Orderrs.First(o => o.Acr == 0);
order.Acr = 1;
db.SubmitChanges();


this doesnt, because it tries to insert the objects as new, instead of updating.

Code: Select all
protected void Page_Load(object sender, EventArgs e)
{
   Orderr[] orders = getOrders();

   umbrellaDataContext.umbrellaDataContext db = new umbrellaDataContext.umbrellaDataContext();

   foreach (Orderr order in orders)
   {
      order.Acr = 1;
      db.SubmitChanges();
   }


}

private Orderr[] getOrders()
{
   umbrellaDataContext.umbrellaDataContext db = new umbrellaDataContext.umbrellaDataContext();

   var orderrs = from o in db.Orderrs
              select o;

   return orderrs.ToArray();
}


This is also discussed here:

http://forums.asp.net/p/1206448/2113379.aspx

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2543593&SiteID=1

http://forums.microsoft.com/msdn/ShowPost.aspx?postid=2524396&siteid=1

http://msdn.microsoft.com/en-us/library/bb546187.aspx
yuri@wallie.com
 
Posts: 27
Joined: Mon 08 Sep 2008 16:37

experiment using attach()

Postby yuri@wallie.com » Mon 13 Oct 2008 11:11

Hi,

I've tried the following to accomplish an update method on each object, but it does nothing (not even an insert/update or exception), just plain nothing:

Code: Select all
public static void Update(Orderr orderr)
{
   using (umbrellaDataContext db = new umbrellaDataContext())
   {
      db.Orderrs.Attach(orderr, true);
      db.SubmitChanges();

   }
}


So for the moment i think for an update() method, i have to retrieve the orginal object from the DB, then loop through the properties of the current and the original object, and then update it, and save it.
yuri@wallie.com
 
Posts: 27
Joined: Mon 08 Sep 2008 16:37

sollution found

Postby yuri@wallie.com » Mon 13 Oct 2008 12:35

Well, there seems to be a sollution for N-tier applications.

I can update a disconnected object the following way :

db.Orderrs.Attach(orderr,true);
orderr.Description = "updatetest";
db.SubmitChanges();

The trick is first to attach the object, and THEN change the properties.

The result is the following SQL:

Code: Select all
UPDATE umbrella.orderr SET description = 'updatetest' WHERE `orderrPK` = '03cd20ea-4839-4db3-b2b1-fd677151e52c'
yuri@wallie.com
 
Posts: 27
Joined: Mon 08 Sep 2008 16:37

Postby Shalex » Mon 20 Oct 2008 09:00

We are investigating this issue. You will be notified on the results as soon as possible.
Shalex
Devart Team
 
Posts: 7293
Joined: Thu 14 Aug 2008 12:44


Return to dotConnect for MySQL