2

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
        }
    ]
}
roydukkey
  • 3,149
  • 2
  • 27
  • 43
  • This might be a stupid question but you didn't mention it in your question. Do you have the unique constraints bundle active on the server? – ashansky Dec 14 '15 at 20:04
  • I've seen this [post](http://stackoverflow.com/questions/29648566/ensuring-unique-email-address-in-documents), but I'm not understanding how it was solved. I also don't see the `Unique Constraints` bundle in the list when creating a new db resource. – roydukkey Dec 14 '15 at 20:07
  • it looks like this bundle is separate. See here: http://ravendb.net/docs/article-page/3.0/csharp/server/bundles/unique-constraints – ashansky Dec 14 '15 at 20:09
  • Okay. Then here is my next question. How do I get it? – roydukkey Dec 14 '15 at 20:11
  • In my last download of RavenDB there's a folder called "Bundles" on the top level. And that includes the UniqueConstraint bundle. – ashansky Dec 14 '15 at 20:15
  • Okay. I've added that to the `~/Plugins/Raven.Bundles.UniqueConstraints.dll`, but still not exception for non-unique data. And, still the bundle isn't listed in studio. – roydukkey Dec 14 '15 at 20:33
  • It seems that `Raven.Bundles.UniqueConstraints.dll` `RavenDB/Plugins`and `RavenDB/Databases/MVC6Test/Plugins`. However, I cannot find any documentation on this. – roydukkey Dec 14 '15 at 23:03

0 Answers0