Sorry but i'm a beginner.
I have two classes: User and Group which are linked together (Group class have an User type attribute)
Here is User.cs :
public class User
{
[Key]
public int userId { get; set; }
[Required]
[MaxLength(10)]
[Display(Name="Firstname")]
public string firstname { get; set; }
[Required]
[MaxLength(10)]
[Display(Name = "Lastname")]
public string lastname { get; set; }
}
Here is Group.cs :
public class Group
{
[Key]
public int idGroup { get; set; }
public string name { get; set; }
public User owner { get; set; }
}
Here is the insert
private myContext db = new myContext();
[HttpPost]
public ActionResult Create(Group group)
{
group.owner = (User)Session["user"];
if (ModelState.IsValid)
{
db.Groups.Add(group);
db.SaveChanges();
return RedirectToAction("Index","Home");
}
return View(group);
}
//Session["user"] contains an User type object (which is already in database)
The problem is : When I add a new group, it automatically inserts a new User into the databae, instead of JUST inserting the corresponding group.
Example : I am "user1" (so "user1" is present in the User table) and I want to create the "group1" group. I link the user I am with the "owner" attribute of the Group class.
group1 will be inserted into Group table but user1 will be also inserted into the User table.. That will cause a duplicate content
I don't know how to avoid this...
Any help would be appreciated.