1

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?

Dennis Nerush
  • 5,473
  • 5
  • 25
  • 33
  • In other words, no two records can have the same email. Two records can have the same GoogleId and FacebookId, but not the same combination? – Yaakov Ellis Aug 26 '13 at 08:49
  • Yes.Lets say that those two entities will violate the unique constraint: new Person("John", "1@a.com", "googleId1", "facebookId1") new Person("Avi', "2@a.com", "googleId1", "facebookId1"); – Dennis Nerush Aug 27 '13 at 09:05
  • But setting a unique constraint on all 3 wont work, because email by itself is always a unique item, right? – Yaakov Ellis Aug 27 '13 at 12:04
  • Yes. Both the email and the two other properties are unique constraints. In total I have 2 unique constraints- one on the email and another on the googleId + facebookId. The main question is how to set a unique constraint on the googleId + facebookId? – Dennis Nerush Aug 27 '13 at 12:08

2 Answers2

1

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;
    }
}
0

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

Jevgenij Nekrasov
  • 2,690
  • 3
  • 30
  • 51