The classes now are:
public class ApplicationUser : IdentityUser
{
// HomeTown will be stored in the same table as Users
public string HomeTown { get; set; }
public virtual ICollection ToDoes { get; set; }
public virtual ICollection ToDoes1 { get; set; }
public virtual MyUserInfo MyUserInfo { get; set; }
}
public class ToDo
{
public int Id { get; set; }
public string Description { get; set; }
public bool IsDone { get; set; }
public virtual ICollection<ApplicationUser> User { get; set; }
public virtual ICollection<ApplicationUser> User1 { get; set; }
}
Initialization file now is like:
private void InitializeIdentityForEF(MyDbContext context)
{
var UserManager = new UserManager(new UserStore(context));
var RoleManager = new RoleManager(new RoleStore(context));
var myinfo = new MyUserInfo() { FirstName = "Pranav", LastName = "Rastogi" };
string name = "Admin";
string password = "123456";
string test = "test";
//Create Role Test and User Test
RoleManager.Create(new IdentityRole(test));
var user1 = new ApplicationUser();
user1.UserName = test;
user1.HomeTown = "Seattle";
user1.MyUserInfo = myinfo;
user1.ToDoes = new List<ToDo>();
UserManager.Create(user1, password);
//Create Role Admin if it does not exist
if (!RoleManager.RoleExists(name))
{
var roleresult = RoleManager.Create(new IdentityRole(name));
}
//Create User=Admin with password=123456
var user = new ApplicationUser();
user.UserName = name;
user.HomeTown = "Seattle";
user.MyUserInfo = myinfo;
user.ToDoes = new List<ToDo>();
var adminresult = UserManager.Create(user, password);
//Add User Admin to Role Admin
if (adminresult.Succeeded)
{
var result = UserManager.AddToRole(user.Id, name);
}
var todo = new List<ToDo>
{
new ToDo { Description = "teste1", IsDone = false, User = new List<ApplicationUser>()},
new ToDo { Description = "teste2", IsDone = false, User = new List<ApplicationUser>()},
};
todo.ForEach(s => context.ToDoes.Add(s));
context.SaveChanges();
todo[0].User.Add(user);
context.SaveChanges();
user.ToDoes.Add(todo[0]);
context.SaveChanges();
user.ToDoes.Add(todo[1]);
context.SaveChanges();
var count1 = user.ToDoes.Count; //Here the count is 2
var count2 = user1.ToDoes.Count; //Here the count is 0
user1.ToDoes.Add(todo[0]);
context.SaveChanges();
user1.ToDoes.Add(todo[1]);
context.SaveChanges();
var count3 = user.ToDoes.Count; //Here the count is 0
var count4 = user1.ToDoes.Count; //Here the count is 2
}
The error:
count1 and count3 must be 2, however when todo[0] and todo[1] are added to user1, they are removed from user todo list.
Is this a bug or a error on the code?
If I delete ToDoes1 from ApplicationUser class and User1 from ToDo class the error goes away. However my project will need it.
The classes now are: public class ApplicationUser : IdentityUser { // HomeTown will be stored in the same table as Users public string HomeTown { get; set; } public virtual ICollection ToDoes { get; set; }
public virtual ICollection ToDoes1 { get; set; }
Initialization file now is like: private void InitializeIdentityForEF(MyDbContext context) { var UserManager = new UserManager(new UserStore(context));
var RoleManager = new RoleManager(new RoleStore(context));
var myinfo = new MyUserInfo() { FirstName = "Pranav", LastName = "Rastogi" };
string name = "Admin";
string password = "123456";
string test = "test";
The error: count1 and count3 must be 2, however when todo[0] and todo[1] are added to user1, they are removed from user todo list. Is this a bug or a error on the code? If I delete ToDoes1 from ApplicationUser class and User1 from ToDo class the error goes away. However my project will need it.