开发者

PLINQO / LINQ-To-SQL - Generated Entity Self Save Method?

开发者 https://www.devze.com 2023-01-15 08:26 出处:网络
Hi I\'m trying to create a basic data model / layer The idea is to have: Task task = TaskRepository.GetTask(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);


                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
    }


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号