防止添加子数据实体框架

时间:2016-09-06 12:16:33

标签: entity-framework

我有这种类结构。

public class Company :EntityBase
{

    [StringLength(60, MinimumLength = 3)]
    [Required(ErrorMessage = "Name required-Cant be Blank")]
    public string Name { get; set; }

    [Required(ErrorMessage = " required-Cant be Blank")]
    public bool AnimalTesting { get; set; }

    public int? ParentCompanyId { get; set; }

    [ForeignKey("ParentCompanyId")]
    public virtual ParentCompany ParentCompany { get; set; }

    public Company()
    {

    }


}

 public class ParentCompany : EntityBase
{

    public string Name { get; set; }

    public bool Testing { get; set; }

    public virtual ICollection<Company> Company { get; set; }
}

现在我正在创建像这样的公司实体。

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,Name,AnimalTesting,ParentCompany")] Company company)
    {
        if (ModelState.IsValid)
        {
            List<ParentCompany> parents = (List<ParentCompany>)unitOfWork.ParentCompanyRepository.List
                (o => o.Name == company.ParentCompany.Name);


            if (parents.Count==0)
            {
               unitOfWork.ParentCompanyRepository.Add(company.ParentCompany);
               unitOfWork.CompanyRepository.Add(company);
            }
            else
            {
                IMyContext context;
                context = unitOfWork.GetDbContext<VeganDbContext>();
                context.Entry(company.ParentCompany).State = EntityState.Unchanged;
                context.Entry(company).State = EntityState.Added;
                context.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        return View(company);
    }

当我想在数据库中添加现有ParentCompany的公司时,程序转到其他括号。现在我收到错误,但是可以说我无法添加当前存在于数据库中的相同值。

  

INSERT语句与FOREIGN KEY约束冲突   &#34; FK_dbo.Company_dbo.ParentCompany_ParentCategoryId&#34 ;.冲突   发生在数据库中   &#34; ASPNET_VEGAN_20150428012206_c73db9e12d424dfd80e198ac36a739cf&#34;,表   &#34; dbo.ParentCompany&#34;,列&#39; Id&#39;。声明已经终止。

但是:)现在有我的观点。 我想将数据库中的现有字段添加到我的公司,但我不想将相同的ParentCompany添加到数据库。

任何想法如何实现我的观点?

1 个答案:

答案 0 :(得分:0)

Nvm我找到了解决方案:D

public ActionResult Create([Bind(Include = "Id,Name,AnimalTesting,ParentCompany")] Company company)
    {
        List<Company> ExistCompany = (List<Company>)unitOfWork.CompanyRepository.List
                (o => o.Name == company.Name);

        if (ModelState.IsValid && ExistCompany.Count==0)
        {
            List<ParentCompany> Parents = (List<ParentCompany>)unitOfWork.ParentCompanyRepository.List
                (o => o.Name == company.ParentCompany.Name);


            if (Parents.Count==0)
            {
               unitOfWork.ParentCompanyRepository.Add(company.ParentCompany);
               unitOfWork.CompanyRepository.Add(company);
            }
            else
            {
                foreach (ParentCompany p in Parents)
                {
                    company.ParentCompany = p;
                    break;
                }
                IMyContext context;
                context = unitOfWork.GetDbContext<VeganDbContext>();
                context.Entry(company.ParentCompany).State = EntityState.Unchanged;
                context.Entry(company).State = EntityState.Added;
                context.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        if(ExistCompany.Count!=0)
           ViewBag.ExistCompany = "Company with that name exist ";

        return View(company);
    }