0

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alex Do
  • 3
  • 1

2 Answers2

0

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Thank you Marc. I'd checked that method carefully. I still don't know why this crap happen. Everything is okay til SubmitChanges() is executed. All my fields have enough set & get method as well. – Alex Do Oct 25 '09 at 10:17
  • Since my code is too long so that I can't bring them all here. Anyway, almost of columns are set with the following properties: [Column(Name = "i_artist_id", Storage = "_ArtistID", DbType = "Int NOT NULL DEFAULT 0", CanBeNull = false, UpdateCheck = UpdateCheck.Never)] What do you mean about 'readonly' property? I found no such thing like this. There is also no 'readonly' field in my database. Thank you Marc! – Alex Do Oct 25 '09 at 10:57
  • Marc, I checked, but all Read-Only are false. Is there any reasonable way? – Alex Do Oct 25 '09 at 14:02
0

After making a lot of tests, I found one thing: all foreign keys are not updated, normal fields are okay. So tell me is there any reasonable way caused this crap?

I googled a lot but found nothing...

Alex Do
  • 3
  • 1