Question

Let's say I have a class:

public class Person
{
   public string Name{get;set;}
   public string Email {get;set;}
   public string GoogleId {get;set;}
   public string FacebookId {get;set;}
}

If I want to make the email unique I will use the unique constraint bundle.

But I want to make both the googleId and the facebookId properties as a single unique constraint side by side with the email constraint (while non of them is the id). Is it possible?

Was it helpful?

Solution

Use the UniqueConstraints bundle:

public class Person
{
   public string Name {get;set;}
   [UniqueConstraint]
   public string Email {get;set;}
   public string GoogleId {get;set;}
   public string FacebookId {get;set;}
   [UniqueConstraint]
   public string GoogleAndFacebookIds { get;set; }
}

Just make sure you update GoogleAndFacebookIds everytime you update either GoogleId or FacebookId. I was doing this so much I ended up using a simple interface on all my classes that did this sort of thing:

public interface ICombinedConstraints
{
    string UniqueId { get; set; }
    void UpdateConstraints();
}

So,

public class Person : ICombinedConstraints
{
    public string Name{get;set;}
    [UniqueConstraint]
    public string Email {get;set;}
    public string GoogleId {get;set;}
    public string FacebookId {get;set;}
    [UniqueConstraint]
    public string UniqueId { get; set; }

    public void UpdateConstraints()
    {
        UniqueId = GoogleId + FacebookId;
    }
}

OTHER TIPS

You cannot do that. Raven doesn't provide any possibility to enforce unique constraints on the property. If you want to do that you need separate sets of documents (see link below)

Unique constraints in Raven

Update:

It seems that you can also try to use bundles to implement unique constraints in the object.

Bundle: Unique constraints

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top