sontek
sontek

Reputation: 12451

How do you update an object with Linq 2 SQL without rowversion or timestamp?

I'm trying to take a POCO object and update it with Linq2SQL using an XML mapping file... This what what I have:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business.Objects
{
    public class AchievementType
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}


<?xml version="1.0" encoding="utf-8" ?>
<Database Name="Name" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
  <Table Name="dbo.AchievementTypes" Member="Business.Objects.AchievementType">
    <Type Name="Business.Objects.AchievementType">
      <Column Name="Id" Member="Id" IsDbGenerated="true" IsPrimaryKey="true" />
      <Column Name="Name" Member="Name" />
    </Type>
  </Table>
</Database>

CREATE TABLE AchievementTypes
(
    Id          INTEGER         IDENTITY NOT NULL,
    Name        NVARCHAR(255)   NOT NULL,

    CONSTRAINT PK_AchievevementTypes 
        PRIMARY KEY (Id),

    CONSTRAINT UQ_AchievementTypes
        UNIQUE (Name)
)

and i'm doing the following to update it:

var type_repo = new BaseRepository<AchievementType>();
var t1 = new AchievementType { Name = "Foo" };
type_repo.Insert(t1);
t1.Name = "Baz";
type_repo.Save(t1, "Id");

and my repository Save is just doing:

public void Update(TData entity)
{
    using (var ctx = MyDataContext())
    {
        ctx.GetTable<TData>().Attach(entity);
        ctx.SubmitChanges();
    }
}

The update doesn't fail or anything, but the data in the database has not changed.

Upvotes: 0

Views: 1559

Answers (1)

sontek
sontek

Reputation: 12451

Bah, right after I asked I found some documentation on it.

Since Context isn't tracking the changes, you need to do the following:

ctx.Refresh(RefreshMode.KeepCurrentValues, entities);

Upvotes: 2

Related Questions