views:

14

answers:

2

Hi I'm trying to create a basic data model / layer

The idea is to have:

Task task = TaskRepository.GetTask(2);

task.Description = "The task has changed";

task.Save();

Is this possible? I've tried the code below

Note: The TaskRepository.GetTask() methods detaches the Task entity.

I'd expect this to work, any ideas why it doesnt?

Thanks

    public partial class Task
    {
        // Place custom code here.

        public void Save()
        {
            using (TinyTaskDataContext db = new TinyTaskDataContext { Log = Console.Out })
            {
                db.Task.Attach(this);
                Task task = new Task();

                db.SubmitChanges();
            }
        }


        #region Metadata
        // For more information about how to use the metadata class visit:
        // http://www.plinqo.com/metadata.ashx
        [CodeSmith.Data.Audit.Audit]
        internal class Metadata
        {
             // WARNING: Only attributes inside of this class will be preserved.

            public int TaskId { get; set; }

            [Required]
            public string Name { get; set; }

            [Now(EntityState.New)]
            [CodeSmith.Data.Audit.NotAudited]
            public System.DateTime DateCreated { get; set; }

        }

        #endregion
    }
A: 

Hello,

There is no need for this line (Task task = new Task();). The above should work although I've never seen it implemented in this manner. Have you thought about using the managers? Are you running into any runtime errors?

Thanks -Blake Niemyjski

Blake Niemyjski
Sorry, my mistake, I should of deleted Task task = new Task();
Mantisimo
I wish to work in a disconnected manner. From what I've seen arent mangers tied to the context?...So in effect i'd have the same problem?
Mantisimo
I've speant the night reading about implementing PLINQO with the repository patern. http://jonkruger.com/blog/2008/02/10/linq-to-sql-in-disconnectedn-tier-scenarios-saving-an-object/
Mantisimo
+1  A: 

Having done some reading I've realised I was implmenting the Repository pattern incorrectly. I should have been adding the Save method to the repository for conventions sake.

However, the actually problem I was having with regard to commiting the disconnected dataset was due to optimistic concurrency. The datacontext's job is to keep track of the state of it's entities. When entities become disconnected you loose that state.

I've found you need to add a timestamp field to the database table or I can set the UpdateCheck field on each column in my dbml file.

Here is some info about the UpdateCheck

Some useful links about disconnected Linq and plinqo

Great info on implementing the Repository pattern with LINQ

Short tutorial for implementing for updating and reattaching entities

Previously answer question

Rick Strahl on LINQ to SQL and attaching Entities

Mantisimo