实体框架,将列更改为只读

发布于 2024-11-01 06:39:38 字数 539 浏览 0 评论 0原文

我正在使用 .NET 4.0、Entity Framework 4 和 SQL Server 2005。

我有一个现有的 EF 模型。 名为 Order 的实体之一有一个名为 Name (varchar 255) 的列,该列上有一个唯一键。

过去,此列中的值是由最终用户在 Web 表单上提供其值来确定的。在提交表单之前,会与数据库中的其他值进行检查,以确保值是唯一的。

要求已更改,因此现在该列的值将在第一次创建订单时计算,此后不再更改。该计算涉及对具有相同值的字段 VariableNumber (varchar 255) 的现有订单进行计数。例如:

int count = this.Orders.Where(o => o.VariableNumber == variableNumber).Count();
++count;
return string.Format("{0}-{1:000}", variableNumber, count);

我的问题是:我应该在哪里放置此逻辑以确保首次创建订单时计算名称?

谢谢。

I am using .NET 4.0, Entity Framework 4, and SQL Server 2005.

I have an existing EF model.
One of the Entities named Order has a column named Name (varchar 255) which has a unique key on it.

In the past the value in this column was determined by end users providing its value on a web form. The value was checked against others in the database to ensure a unique value before the form could be submitted.

Requirements have changed so that now this column's value is to be calculated the first time the Order is created, and never changed afterwards. The calculation involves counting the number of existing Orders that have a field VariableNumber (varchar 255) with the same value. For Example:

int count = this.Orders.Where(o => o.VariableNumber == variableNumber).Count();
++count;
return string.Format("{0}-{1:000}", variableNumber, count);

My question is this: where do I put this logic to ensure the Name is calculated when the Order is first created?

Thank You.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私野 2024-11-08 06:39:38

一种方法是在数据库触发器中执行此操作。另一种方法是在重写的 SaveChanges 中执行此操作:

public override void SaveChanges()
{
    var orders = context.ObjectStateManager
                        .GetObjectStateEntries(EntityState.Added)
                        .Select(e => e.Entity)
                        .OfType<Order>();

    if (orders.Count() > 0)
    {
        // serialized transaction to lock records so
        // that concurrent thread can't insert orders
        // with the same name while this threads preparing
        // its orders
        using (var scope = new TransactionScope())
        {
            // Here get current counts for variable numbers

            foreach (var order in orders)
            {
                order.Name = ...;
            } 

            base.SaveChanges();
            scope.Complete();
        }
    }
    else
    {
        // saving changes with default transaction
        base.SaveChanges();
    }
}

One approach is doing this in a database trigger. Another approach is doing this in overriden SaveChanges:

public override void SaveChanges()
{
    var orders = context.ObjectStateManager
                        .GetObjectStateEntries(EntityState.Added)
                        .Select(e => e.Entity)
                        .OfType<Order>();

    if (orders.Count() > 0)
    {
        // serialized transaction to lock records so
        // that concurrent thread can't insert orders
        // with the same name while this threads preparing
        // its orders
        using (var scope = new TransactionScope())
        {
            // Here get current counts for variable numbers

            foreach (var order in orders)
            {
                order.Name = ...;
            } 

            base.SaveChanges();
            scope.Complete();
        }
    }
    else
    {
        // saving changes with default transaction
        base.SaveChanges();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文