RavenDB UniqueConstraint doesn't seem to work

与世无争的帅哥 提交于 2020-01-15 03:34:06

问题


I've been trying for a day to get UniqueConstraint working, but it doesn't seem the are. I have a simple MVC6 site that creates a User on a POST. I'm expecting that on the second POST an exception should be thrown as a user will have already been created with the same properties. I'm wanting to ensure that the email address is unique.

using Raven.Client;
using Raven.Client.Document;
using Raven.Client.UniqueConstraints;

namespace MVC6Test.DomainModel
{
    public class User
    {
        public string Id { get; private set; }
        [UniqueConstraint]
        public string Email { get; set; }
        public string Password { get; set; }
        public string Name { get; set; }
    }
}

namespace MVC6Test.Web.Controllers
{
    public class AdminController : Microsoft.AspNet.Mvc.Controller
    {
        private IDocumentStore _documentStore { get; set; }
        public IDocumentSession Session { get; set; }

        [HttpPost]
        [AllowAnonymous]
        [Route("login")]
        public async Task<IActionResult> Login(string userName, string password)
        {
            User user = new User() {
                Email = "test@gmail.com"
            };

            Session.Store(user);
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (_documentStore.IsDefault()) {
                _documentStore = context.HttpContext.RequestServices.GetRequiredService<IDocumentStore>();
            }
            Session = _documentStore.OpenSession();
            base.OnActionExecuting(context);
        }

        public override void OnActionExecuted(ActionExecutedContext context)
        {
            using (Session) {
                if (Session != null && context.Exception == null) {
                    Session.SaveChanges();
                }
            }
            base.OnActionExecuted(context);
        }
    }
}

namespace MVC6Test.Web
{
    public class Startup
    {
        private IDocumentStore DocumentStore;

        public void ConfigureServices(IServiceCollection services)
        {
            DocumentStore = new DocumentStore {
                DefaultDatabase = "MVC6Test",
                Url = "http://localhost:3366"
            };
            DocumentStore.Listeners.RegisterListener(new UniqueConstraintsStoreListener());
            DocumentStore.Initialize();

            services.TryAddSingleton(typeof(IDocumentStore), (provider) => {
                return DocumentStore;
            });
        }

        public void Configure(IApplicationBuilder app, IApplicationLifetime lifetime)
        {
            lifetime.ApplicationStopped.Register(() => {
                DocumentStore.Dispose();
            });
        }
    }
}

I do get this metadata on the items that are created:

{
    "Raven-Entity-Name": "Users",
    "Raven-Clr-Type": "MVC6Test.DomainModel.User, MVC6Test",
    "Ensure-Unique-Constraints": [
        {
            "Name": "Email",
            "CaseInsensitive": false
        }
    ]
}

来源:https://stackoverflow.com/questions/34275540/ravendb-uniqueconstraint-doesnt-seem-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!