EF并发处理与模式第一种方法时间戳属性

编程入门 行业动态 更新时间:2024-10-26 06:31:34
本文介绍了EF并发处理与模式第一种方法时间戳属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图实施处理并发实体框架在ASP.NET MVC应用程序。

文章说:

在模型\ Department.cs,添加一个跟踪属性:

Adding a Tracking Property to the Department Entity

In Models\Department.cs, add a tracking property: 1685373186 public Byte[] Timestamp { get; set; }

     

时间戳属性指定此列将包括更新的Where子句中,并删除发送到数据库的命令。

The Timestamp attribute specifies that this column will be included in the Where clause of Update and Delete commands sent to the database.

由于我使用的型号第一个的办法,我也跟着步骤1 - 5中的创建一个时间戳列实体框架

Since I'm using a model first approach, I followed steps 1 - 5 outlined in Creating a Timestamp column with Entity Framework

  • 添加一个名为时间戳属性的实体EF模型
  • 设置类型为二进制
  • 设置为空的假
  • 设置StoreGeneratedPattern式来计算
  • 设置ConcurrencyMode为固定
  • 当我从数据库模型更新我的code时,模型\ Department.cs现在包含

    When I update my code from the database model, the Models\Department.cs now contains

    public virtual byte[] Timestamp { get; set; }

    然后我用了元数据类指定时间戳属性:

    Then I used the metadata class to specify the Timestamp attribute:

    // Metadata for Department entity public class DepartmentMetadata { 1685373186 public byte Timestamp { get; set; } }

    Q1。我测试是否时间戳值列正在发生变化的情况下排的编辑。事实并非如此。

    Q1. I tested whether the value of the Timestamp column is changing in case of row edits. It isn't.

    修改1 的问题造成的,因为SQL Server列类型是二,它应该是类型时间戳。改变数据类型固定timestamp列的问题没有得到更新。

    EDIT 1 The problem was caused because the SQL Server column type was binary, where it should have been of type timestamp. Changing the data type fixed the issue of the Timestamp column not getting updated.

    Q2。我试图修改同一个实体(使用打开新的标签),以查看是否 OptimisticConcurrencyException 被抛出。事实并非如此。我究竟做错了什么?请帮助我了解,谢谢。

    Q2. I was trying to modify the same entity (using "Open in new tab") to see if an OptimisticConcurrencyException is thrown. It isn't. What am I doing wrong? Please help me understand, thanks.

    推荐答案

    的问题造成的,因为我有每个HTTP会话只有一个ObjectContext的,所以打开一个新的标签和测试,将不会引起并发异常。

    The problem was caused because I had one ObjectContext per HTTP session, and so opening a new tab and testing it would NOT cause a concurrency exception.

    更多推荐

    EF并发处理与模式第一种方法时间戳属性

    本文发布于:2023-05-29 23:13:04,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/355150.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:种方法   属性   模式   时间   EF

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!