Reputation: 990
I have a simple model of Motorcycle and Person. Many people can rent one motorcycle, so it's a one-to-many relationship.
I have this set as a Repository with Unit of work (code below).
Now I have a motorcycle already in my database (MotorcycleId = 1), and I would like to insert a list of people that have rented this motorcycle. The problem is that every time I insert a list of Person a new motorcycle is also inserted.
How can I insert a Person with an existing motorcycle?
Models:
namespace CodeFirst.Model
{
using System.Collections.Generic;
public class Motorcycle
{
public int MotorcycleId { get; set; }
public int ModelYear { get; set; }
public int Displacement { get; set; }
public decimal Killowatts { get; set; }
public string ModelName { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
}
namespace CodeFirst.Model
{
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual Motorcycle Motorcycle { get; set; }
}
}
Repository method for adding list of entities:
public void Add(List<T> entityList)
{
foreach (T item in entityList)
{
DbEntityEntry dbEntityEntry = _dbContext.Entry(item);
if (dbEntityEntry.State != EntityState.Detached)
{
dbEntityEntry.State = EntityState.Added;
}
else
{
_dbSet.Add(item);
}
}
}
Test method, get motorcycle with id=1 and attach it to each person:
static void AddPersons()
{
var unitOfWork = new UnitOfWork.UnitOfWork();
var personList = new List<Model.Person>();
var motorcycle = unitOfWork.Motorcycles.GetById(1);
personList.Add(new Model.Person() { FirstName = "Jim", LastName = "Morrison", Motorcycle = motorcycle });
personList.Add(new Model.Person() { FirstName = "Jimmy", LastName = "Hoffa", Motorcycle = motorcycle });
personList.Add(new Model.Person() { FirstName = "Arlo", LastName = "Guthrie", Motorcycle = motorcycle });
unitOfWork.Persons.Add(personList);
unitOfWork.Commit();
}
Upvotes: 0
Views: 59
Reputation: 828
Check the relationships between tables in your db and make sure that everything is ok,then change the code and try this:
namespace CodeFirst.Model
{
using System.Collections.Generic;
public class Motorcycle
{
public int MotorcycleId { get; set; }
public int ModelYear { get; set; }
public int Displacement { get; set; }
public decimal Killowatts { get; set; }
public string ModelName { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
}
namespace CodeFirst.Model
{
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual Motorcycle Motorcycle { get; set; }
[ForeignKey("Motorcycle")]
public int MotorcycleId { get; set; }
}
}
and :
static void AddPersons()
{
var unitOfWork = new UnitOfWork.UnitOfWork();
var personList = new List<Model.Person>();
//var motorcycle = unitOfWork.Motorcycles.GetById(1); //not required
var motorcycleId = 1;
personList.Add(new Model.Person() { FirstName = "Jim", LastName = "Morrison", MotorcycleId = motorcycleId });
personList.Add(new Model.Person() { FirstName = "Jimmy", LastName = "Hoffa", MotorcycleId = motorcycleId });
personList.Add(new Model.Person() { FirstName = "Arlo", LastName = "Guthrie", MotorcycleId = motorcycleId });
unitOfWork.Persons.Add(personList);
unitOfWork.Commit();
}
Upvotes: 1
Reputation: 2442
try this
static void AddPersons()
{
var unitOfWork = new UnitOfWork.UnitOfWork();
var motorcycle = unitOfWork.Motorcycles.GetById(1);
motorcycle.Persons.Add(new Person{FirstName = "Jim", LastName = "Morrison"});
unitOfWork.Commit()
}
Upvotes: 1