I am new to Entity Framework. I have a simple class:
public class User { [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string Email { get; set; } public string UserName { get; set; } public string Password { get; set; }
And my Initializer class:
public class UserDataInitializer : DropCreateDatabaseAlways<UserDataContext> { protected override void Seed(UserDataContext context) { var Users = new List<User> { new User() {UserId=1, Email=" a1@gmail.com ", UserName="a1", Password="123456"}, new User() {UserId=2, Email=" a2@gmail.com ", UserName="a2", Password="123456"}, new User() {UserId=3, Email=" a3@gmail.com ", UserName="a3", Password="123456"}, new User() {UserId=4, Email=" a4@gmail.com ", UserName="a4", Password="123456"}} Users.ForEach(i => context.Users.Add(i)); context.SaveChanges(); } }
Suppose I want to add a new user with UserId = 100. It will automatically change the value of UserId to 5.
How can I disable this modification of EF? 'UserId' must be the primary key, and type - int.
source share