Reputation: 3129
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 == "[email protected]")
throw new Exception("Email already exists.");
Upvotes: 1
Views: 1824
Reputation: 93424
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.
Upvotes: 3
Reputation: 46008
The best way to enforce uniqueness in DB is to use unique constraint.
Upvotes: 0
Reputation: 171168
Well, this won't work because you need to declare what you want to enforce uniqueness on. On the primary key? On some other column like "UserName"? On the User.EMail column, but only if it is not null (so you can have multiple users with a null EMail at the same time)?
I prefer such things to be explicit. So I'd actually prefer the logic that you had previously.
Upvotes: 2