문제

We are using Entity Framework 4.1 Code first.

We've got user entity with primary key set to UserId and need UserLogin to be unique. How can it be done?

도움이 되었습니까?

해결책

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; }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top