linqlinq-to-sqlsubmitchanges

SubmitChanges() update only 1 field?


I'm really a LINQ newbie. I got an unknown problem:

public static int save(TEntity obj)
    {
        var table = dbo.GetTable<TEntity>();
        var mapping = dbo.Mapping.GetTable(typeof(TEntity));
        var pkfield = mapping.RowType.DataMembers.Where(d => d.IsPrimaryKey).Take(1).SingleOrDefault();
        if (Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, null)) == 0)
            table.InsertOnSubmit(obj);
        try
        {
            dbo.SubmitChanges();
        }
        catch (ChangeConflictException e)
        {
            dbo.SubmitChanges();
        }
        if (dbo.ChangeConflicts.Count == 0)
        {
            ClearCache(dbo);
            return Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, null));
        }
        else
        {
            dbo.ChangeConflicts.ResolveAll(System.Data.Linq.RefreshMode.KeepCurrentValues);
            return 0;
        }
    }

When using this method, there is only 1 field has been updated!! Here is my log:

UPDATE [dbo].[tbl_album]
SET [dt_m_date] = @p1
WHERE [i_album_id] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input BigInt (Size = 0; Prec = 0; Scale = 0) [1256485605]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4918

Even I changed almost fields, my table has primary key already. But still problem.

Please help!


Solution

  • Did you step through your save method to see what the code was doing? Did it find the right primary key column, and did it indeed detect the presence of the existing primary key?

    What fields to you have in your tbl_album - did you make sure they're not all marked as read-only (unlikely, but still - check to be sure!).

    I don't see antyhing fundamentally wrong with your code right now, it seems a bit complicated for my taste, but it should work, I believe.

    Marc

    UPDATE:
    Check to make sure your table columns aren't all readonly! :)

    alt text