Comment ajouter champ unique non-clé primaire à l'entité en utilisant Entity Framework 4.1?
-
25-10-2019 - |
Question
Nous utilisons Entity Framework 4.1 Code du premier.
Nous avons entité utilisateur avec jeu de clé primaire à UserId
et le besoin UserLogin
d'être unique. Comment peut-il être fait?
La solution
Entity Framework does not support Unique constraints. You can create them using a SQL Query to generate unique constrains when initializing the database. Write your custom initializer for the model and execute SQL command to generate constrain.
Edit
Now (EF 6.1 onwards )you can easily have unique constrains ,
[Index("UserLoginIndex", IsUnique = True)]
public string UserLogin { get; set; }
Autres conseils
Check out the unique constraints, I think that's what you're looking for?
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow