Since there is not any way to add unique constraint to entities in current latest EF version I came up with an idea. ( I know constraints can be added by embedding SQL )
I added a method to my Repository class :
private IDbSet<T> Entities;
public virtual bool Contains(Func<T, bool> predicate)
{
return Entities.Any(t => predicate(t));
}
I think this will replace my current duplicate prevention logic:
bool already exists = (from t in someSet.Entities
where t.UniqueColumn == entity.UniqueColumn select t).
FirstOrDefault() != null;
if(alreadyExists)
throw new Exception("Entity already exists in DB.");
Is this a good idea, what is the best way to prevent duplicate insertions / updates to database?
EDIT: I edited the Contains method to use a predicate. Now I can write a code like this:
if(repository.Contains(t=> t.Email == "someemail@email.com")
throw new Exception("Email already exists.");
If this is going to be a rare situation, then there’s nothing wrong with placing a unique constraint on the database and then catching the exception that gets generated when you try to insert an existing record.
If this is likely to happen a lot, then I would still place the constraint on the database, but do the check as you are.
FWIW, you should make your method take an
Expression<Func<T, bool>>so that the query does not get converted to an Enumerable.