How to change property values twice or more times ??

// seems that dlinq allows to change property value only once, the same happens when object is created not only loaded from database
// is there any way how to make it work
//for tests i used the standard dlinq sample database

using System;
using System.Data.SqlClient;
using System.Query;
using System.Data.DLinq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LINQConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection(@"Data Source=MODA;Initial Catalog=C:\PROGRAM FILES\LINQ PREVIEW\DATA\NORTHWND.MDF;User ID=mike;Password=*********");
DataContext db = new DataContext(conn);
Table<Customer> Customers = db.GetTable<Customer>();
SqlCommand comm = new SqlCommand("SELECT City FROM Customers WHERE CustomerID = @CustomerId", conn);
SqlParameter param = new SqlParameter("CustomerId", System.Data.SqlDbType.NVarChar, 15);
comm.Parameters.Add(param);

Customer customer =
(from c in Customers
where c.City == "London"
select c).First();

conn.Open();
param.Value = customer.CustomerID;
Assert.AreEqual(Convert.ToString(comm.ExecuteScalar()), customer.City);

customer.City = "Riga";
db.SubmitChanges();
Assert.AreEqual(Convert.ToString(comm.ExecuteScalar()), customer.City);

customer.City = "London";
db.SubmitChanges();
// ups London are lost
Assert.AreEqual(Convert.ToString(comm.ExecuteScalar()), customer.City);

conn.Close();
}
}

[Table(Name = "Customers")]
public partial class Customer
{
private string customerId;
private string city;

[Column(Storage = "customerId", Id = true, AutoGen = true)]
public string CustomerID
{
get { return this.customerId; }
}

[Column(Storage = "city")]
public string City
{
get { return this.city; }
set { this.city = value; }
}
}
}



Answer this question

How to change property values twice or more times ??

  • micr0chip

    I just checked against our current bits, and the Current-Version property values get copied to the Database-Version the cache holds for concurrency checks.

    Adding bug
    Updating bug
    Database: MultipleSubmitChanges; Current: MultipleSubmitChanges2
    Updating bug again
    Database: MultipleSubmitChanges2; Current: MultipleSubmitChanges3

    If you continue to see this problem after our next release, please let us know.



  • iShail.com

    The problem is more complicated. Method SubmitChanges() works properly only once even if the second changed City value will not be the same as initial. I call SubmitChanges() to fill auto incrementing fields from database and I can't see there another solution of this problem than calling SubmitChanges() two, three and more times. And it is important all the time to use one DataContext instance as I want to work with only one objects copy.

  • Umamageswari

    I don't have a copy of LINQ in front of me at the moment so I can't test this. I suspect the issue is the original value of the property which your object is caching. When you start, the original value for .City is "London". When you issue the first db.SubmitChanges(); DLINQ checks the original value (London) against the new value (Riga), sees that the new value is different and issues the update command. Key item: It forgets to reset the original value to the new value.

    When the second SubmitChanges is called, it compares the new value (London) against the original value (London) not the intemediary value (Riga). Since they are the same, it does not feel the need to submit that change to the database.

    If this is the issue, you may have found a bug in the current implementation. It would get nastier if you have nested transactions in the mix here. I would be interested in a response from the team on this item.

    Jim Wooley
    http://devauthority.com/blogs/jwooley/default.aspx



  • shellymm

    Yes, you would need to call SubmitChanges to push your updates back to the database. You can make multiple changes and wait to push the updates back until you are finished if you want.

    You may be runing into the issue of deferred execution. If you wish to cache the results of a query, use the .ToList or .ToArray method as follows:

    Customers custList = (from c in Customers
    where c.City == "London"
    select c).ToList();

    Customer customer = custList.First();

    This will cache your results and will not fire another fetch on each iteration of the list. See if this helps resolve your issue.

    Jim Wooley
    http://devauthority.com/blogs/jwooley/default.aspx



  • wesogs

    Ok, if we are working with one database table and one class type it wouldn't be difficult, but realy I work with many different class objects that are connected by associations, so I need to store dependent objects to database one by one (otherwise I couldn't get to know objects autogenerated primary key properties, to associate abjects). But if SubmitChanges() works correctly only first time, i could make changes to first object only before storing the next. Well I can make changes later, but into database will be stored only the first changes. My provided example are poor, but it shows that SubmitChanges are not working as I supposed to see. If we create associated object structure and only at the end try to call SubmitChanges() we get exception "The insert statement conflicted with the FOREIGN KEY constraint...", so the SubmitChanges() should work well, no matted how much times you are calling it. Anyway this is what I am expecting.

  • How to change property values twice or more times ??