PLINQO / LINQ-To-SQL - 生成的实体自保存方法?
嗨,我正在尝试创建一个基本数据模型/层,
其想法是:
Task task = TaskRepository.GetTask(2);
task.Description = "任务已更改";
任务.保存();
这可能吗?我尝试了下面的代码
注意:TaskRepository.GetTask() 方法分离任务实体。
我希望这能起作用,有什么想法为什么不起作用吗?
谢谢
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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
读了一些书后,我意识到我错误地实现了存储库模式。为了惯例,我应该将 Save 方法添加到存储库中。
然而,我在提交断开连接的数据集时遇到的实际问题是乐观并发性造成的。数据上下文的工作是跟踪其实体的状态。当实体断开连接时,您就会失去该状态。
我发现您需要向数据库表添加时间戳字段,或者我可以在 dbml 文件中的每一列上设置 UpdateCheck 字段。
以下是有关 UpdateCheck
关于断开连接的 Linq 和 plinqo 的一些有用链接
有关使用 LINQ 实现存储库模式的重要信息
实现更新和重新附加实体的简短教程
之前回答的问题
Rick Strahl 谈 LINQ to SQL 和附加实体
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
不需要这一行(Task task = new Task();)。上面的方法应该可以工作,尽管我从未见过它以这种方式实现。您是否考虑过使用经理?您遇到任何运行时错误吗?
谢谢
——布莱克·尼米斯基
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