Several unique constrains on a single document in RavenDb - ravendb

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?

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

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

Related

Asp.net core Add Soft delete by modelBuilder.Entity<DependenciaDisciplina>().Property<bool>("isDeleted"); but isDeleted as composite key

I insert the soft delete flag with
modelBuilder.Entity<DependenciaDisciplina>().Property<bool>("isDeleted");
But i need to add it as a composite key
Can somebody help me?
I insert the soft delete flag with
modelBuilder.Entity<DependenciaDisciplina>().Property<bool>("isDeleted");
But i need to add it as a composite key
To set the Composite Keys using the Fluent API, we have to use the HasKey() method.
After check the HasKey() method definition, we can see that the parameter should be a string array or an expression.
So, you could use the set the composite key like this (change the Car model to your model):
modelBuilder.Entity<Car>().Property<bool>("isDeleted");
modelBuilder.Entity<Car>().HasKey(new string[] { "CarId", "isDeleted" });
The Car model:
public class Car
{
[Key]
public int CarId { get; set; }
public string CarName { get; set; }
public string LicensePlate { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
Then, after migration, the result like this:

cant create a relationship between application user table and my tables

I'm using entity framework code first. The below keeps kicking out the unable to determine the principal end of an association. Basically I can't figure out how to get one to many relationships between the auto generated identity table and my own tables with code first.
public class applicationUser : Identity
{
public User user {get;set;}
}
//and then a dependant class called User
public class User
{
public in Id {get;set;}
public string Name {get;set;}
[ForeignKey("Identity")]
public string IdentityId {get;set;}
public ApplicationUser Identity {get;set;}
public string UserPicLocation {get;set;}
}
You're using [ForeignKey()] wrong. The value of [ForeignKey()] should be the name of the property that contains the key of the referenced object.
You should also mark the property that contains the key in the referenced object as the private key with [Key]
Try this:
public class applicationUser : Identity
{
public int UserId {get; set;}
[ForeignKey("UserId")]
public User user {get;set;}
}
and then another class called User
public class User
{
[Key]
public int Id {get;set;}
public string Name {get;set;}
public string IdentityId {get;set;}
public ApplicationUser Identity {get;set;}
public string UserPicLocation {get;set;}
}
I fixed it by setting the primary key as both the primary and foreign key in the dependant table

PetaPoco mapping properties within properties

I am new to PetaPoco and initially I was liking it but then hit a wall which I simply dont know how to search for.
I have a object which needs to set a property within one of its properties, ie Job.Min.BaseValue. The source of this data is "min_mb".
So basically my object is not a direct mapping of the source table
public class Usage
{
public Decimal BaseValue {get;set;}
public Decimal BaseScale {get;set;}
public Decimal BaseUnit {get;set;}
}
[PetaPoco.TableName("data")]
[PetaPoco.PrimaryKey("date, client_name")]
[PetaPoco.ExplicitColumns]
public class Job
{
[PetaPoco.Column("date")]
public DateTime Date {get;set;}
[PetaPoco.Column("client_name")]
public String ClientName {get;set;}
public Usage Min {get;set;}
public CommvaultJob() { Min = new Usage() { BaseScale=1024, BaseUnit="MB" }; }
}
I think you're just missing the extra type when you call Fetch or Query. This worked for me :
Calling PetaPoco :
var allData = _db.Fetch<TestJobPoco,Usage>("select * from dataTEST");
return View( allData);
The pocos :
[PetaPoco.ExplicitColumns]
public class Usage
{
public Usage()
{
BaseScale=1024;
BaseUnit="MB";
}
[PetaPoco.Column("base_value")]
public Decimal BaseValue {get;set;}
[PetaPoco.Ignore]
public Decimal BaseScale {get;set;}
[PetaPoco.Ignore]
public string BaseUnit {get;set;}
}
[PetaPoco.TableName("dataTEST")]
[PetaPoco.PrimaryKey("id")]
[PetaPoco.ExplicitColumns]
public class TestJobPoco
{
[PetaPoco.Column("id")]
public int Id {get;set;}
[PetaPoco.Column("date")]
public DateTime Date {get;set;}
[PetaPoco.Column("client_name")]
public String ClientName {get;set;}
public Usage Min {get;set;}
public TestJobPoco()
{
//Min = new Usage() { BaseScale=1024, BaseUnit="MB" };
}
}
My test database has an id, date, client_name and base_value columns. The primary key is id so it's slightly different than yours but this shouldn't change the way the poco mapping happens.
If your objects do not map with the table structure, an ORM can't help much.
You will need to do the mapping manually or made new shadow properties that copy the values of the other fields, but this added complexity will defeat the purpose of an ORM.

dapper populate DropDownlist

I have a simple Poco
public virtual short UserID
{
get;
set;
}
[Required]
public virtual string UserName
{
get;
set;
}
public virtual string Password
{
get;
set;
}
public virtual string Email
{
get;
set;
}
Im currently Using Dapper ORM.
Does anyone have a good example of how I would query using dapper ORM to create a drop-down-list?
The query should return Key=UserID and Value=UserName in a list so that I can retrieve the keys and populate the DropDownList.
you can create a class representing the pair:
class SelectItem
{
public long Key {get;set;}
public string Value {get;set;}
}
var list = connection.Query<SelectItem>(" select id Key UserName Value from yourtable",null).ToList();
you use the aliases to map the table fields to the class properties names. I'm supposing your table field names are id and UserName, change them according to your case.
You should also pay attention to the property types, you can have a bad cast exception if they don't match.
ALternatively, you can use the dynamic version:
var list = connection.Query(" select id Key UserName Value from yourtable",null).ToList();
you obtain a list of dynamics each with property named Key and UserName.

EF4 Code Only - Map Columns to Property Complex type

I have a table like this:
Name
Tree
Iron
Clay
Added
I want to map it to a model like this:
Name
Resources
Tree
Iron
Clay
Added
In makes sense to map it like this, when working with it in my program, but doing it that way in the databse would just make it more complex ... not would not add any useful things.
Is it possible with EF4 Code ONly?
public class Sample
{
public int Id { get; set;} // primary key required
public string Name {get;set;}
public DateTime Added{get;set;}
}
public class Resource
{
// no Id defined here
public string Tree{get;set;}
public string Iron { get;set;}
public string Clay { get;set;}
}
public class SampleDB : DbContext
{
//public DbSet<Resource> Resources { get; set; } // should not be there
public DbSet<Sample> Samples { get; set; }
}