在父级已存在的情况下添加实体一对多

时间:2015-11-29 20:10:00

标签: c# entity-framework

我有一个简单的摩托车和人模型。很多人可以租一辆摩托车,所以这是一对多的关系。

我将此设置为具有工作单元的存储库(代码如下)。

现在我的数据库中已有一辆摩托车(MotorcycleId = 1),我想插入租来这辆摩托车的人员名单。问题是每次我插入一个Person列表时,也会插入一个新的摩托车。

如何插入现有摩托车的人员?

型号:

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; }
    }
}

添加实体列表的存储库方法:

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);
        }
    }
}

测试方法,让id = 1的摩托车并将其附在每个人身上:

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();
}

2 个答案:

答案 0 :(得分:1)

试试这个

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()   
}

答案 1 :(得分:1)

检查数据库中表之间的关系,确保一切正常,然后更改代码并尝试:

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; }
    }
}

和:

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();
}