0

我有一个包含 UserAccounts 列表的 excel 文件。我还有一种方法可以导入这些 UserAccounts 并将它们保存到 RavenDB。在 excel 文件中,我存储了 UserAccount 对象 (useraccounts/55) 的 ID。RavenDB 没有分配值,我正在分配它。我的导入效果很好。

然而,

稍后,我尝试使用以下方法通过管理面板保存新的 UserAccount:

       [HttpPost]
            public ActionResult Create(UserAccountViewModel input)
            {
               // Validation omitted             

                    var model = new UserAccount()
                    {
                        Email = input.Email,
                        FirstName = input.FirstName,
                        LastName = input.LastName,
                        Phone = input.Phone,
                        Username = input.Username,
                        AuthorizeNetCustomerProfileId = customer.ProfileID,
                        Password = input.Password,

                    };

                    Raven.Store(model);
                    Raven.SaveChanges();
                    return RedirectToAction("Index");
             }

当我打电话

Raven.Store(model) 

它为新的 UserAccount 对象分配了一个 ID,但它从 1 开始。所以当我第一次尝试这样做时,它会将 UserAccounts/1 分配给我的新 UserAccount。问题是我的导入中已经存在 UserAccounts/1 ,所以当我调用保存更改时,我得到了一个 etag 异常。

当我再次运行该方法时,它会分配 UserAccounts/2 等等?想法?

4

1 回答 1

2

The easiest way would be to have a string Id property in your UserAccount class, and assign it a value of "UserAccounts/". That trailing slash is going to ask RavenDB to assign it an ID using an identity-like process, instead of HiLo. Its somewhat slower, but it'll work.

The better way of solving this is by changing the HiLo documents on the server, making them start with the first available range, but thats messier.

于 2013-01-29T16:45:43.170 回答