实体框架4.1代码优先-非主键插入时的自动递增字段 [英] Entity Framework 4.1 Code First - auto increment field on insert for non primary key

查看:116
本文介绍了实体框架4.1代码优先-非主键插入时的自动递增字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型包含一个Order(父对象)和Shipments(子对象).这些数据库表已经具有代理键作为自动增量主键.

My model contains an Order (parent object) and Shipments (child object). The database table for these already have a surrogate key as an auto-increment primary key.

我的业务规则是,对于订单中的每个发货,我们都需要有一个自动生成的计数器"字段-例如托运1,托运2,托运3等.托运模型具有属性:"ShipmentId","OrderId","ShipmentNumber".我尝试的实现是将ShipmentNumber设置为int并使用代码(与数据库相对),查询Shipment集合并执行max()+ 1.

I have the business rule is that for each shipment in the order, we need to have an auto generated "counter" field -- e.g. Shipment 1, Shipment 2, Shipment 3, etc. Shipment model has properties: "ShipmentId", "OrderId", "ShipmentNumber". My attempted implemention is to have ShipmentNumber an int and in code(as opposed to database), query the Shipment collection and do max() + 1.

这是我在做什么的代码摘要.

Here's a code snipet of what I'm doing.

        Shipment newShipmentObj = // blah;

        int? currentMaxId = myOrderObj.Shipments
                            .Select(x => (int?) x.ShipmentNumber)
                            .Max();
            if (currentMaxId.HasValue)
                newShipmentObj.ShipmentNumber = currentMaxId.Value + 1;
            else
                newShipmentObj.ShipmentNumber = 1; // 1st one

        myOrderObj.Shipments.Add(newShipmentObj);

        // etc.. rest of EF4 code

有更好的方法吗?

我不太喜欢这样做,因为存在潜在的事务/并发问题,因此存在以下问题.

I don't really like this as I have the following problems because of potential transaction/concurrency issues.

我的订单"对象还具有自动递增的计数器",例如订单1,订单2,订单3,...我的订单模型具有属性:订单ID",客户ID",订单编号".

My Order object also has a autoincrement "counter" -- e.g. Order 1, Order 2, Order 3, ... My Order model has properties: "OrderId", "CustomerId", "OrderNumber".

我的设计是我有一个OrderRepository,但没有ShipmentRepository. ShipmentRepository可以查询Order.Shipment集合...但是使用Orders,我必须直接查询dbcontext,例如

My design is that I have an OrderRepository but not a ShipmentRepository. The ShipmentRepository could query off the Order.Shipment collection... but with Orders, I have to query directly off the dbcontext, e.g.

        int? currentMaxId = (_myDbContext)).Orders
                        .Where(x => x.CustomerId == 123456)
                        .Select(x => (int?)x.OrderNumber)
                        .Max();

但是,如果我尝试将多个对象添加到DbContext而不提交/保存对数据库的更改,那么上面的部分将无法正常工作. (即.Where()返回null ...,并且仅当我使用DbContext".Local"才有效,这不是我想要的.)

However, the above part doesn't work well if I attempt to add multiple objects to the DbContext without committing/saving changes to the database. (i.e. the .Where() returns null... and only works if I use DbContext ".Local", which is not what I want.)

帮助!不知道最好的解决方案是什么.谢谢!

Help! Not sure what the best solution would be. Thanks!

推荐答案

您似乎已经具有递增的shippingid.您可以将其用于您的货件编号,也可以与此处描述的当前日期结合使用:

you seem to already have shipmentid that is incremental. you can use it for you shipment number and maybe combined with current date as described here: How to implement gapless, user-friendly IDs in NHibernate? what you are trying to do with Max() is evil. Stay away from it as it can cause problems with getting the same shipment numbers for multiple shipments when the load is high

这篇关于实体框架4.1代码优先-非主键插入时的自动递增字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆