Entity framework 防止添加子数据实体框架
我有这个班级结构Entity framework 防止添加子数据实体框架,entity-framework,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")] pub
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; }
}
公共类公司:EntityBase
{
[StringLength(60,最小长度=3)]
[必需(ErrorMessage=“必需的名称不能为空”)]
公共字符串名称{get;set;}
[必需(ErrorMessage=“必需不能为空”)]
公共布尔动物测试{get;set;}
public int?ParentCompanyId{get;set;}
[ForeignKey(“母公司ID”)]
公共虚拟父公司父公司{get;set;}
上市公司()
{
}
}
公共类父公司:EntityBase
{
公共字符串名称{get;set;}
公共布尔测试{get;set;}
公共虚拟ICollection公司{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);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果创建([Bind(Include=“Id,Name,AnimalTesting,ParentCompany”)]公司)
{
if(ModelState.IsValid)
{
List parents=(List)unitOfWork.ParentCompanyRepository.List
(o=>o.Name==company.ParentCompany.Name);
if(parents.Count==0)
{
unitOfWork.ParentCompanyRepository.Add(company.ParentCompany);
unitOfWork.CompanyRepository.Add(公司);
}
其他的
{
无语境;
context=unitOfWork.GetDbContext();
context.Entry(company.ParentCompany).State=EntityState.Unchanged;
context.Entry(company.State=EntityState.Added;
SaveChanges();
}
返回操作(“索引”);
}
返回视图(公司);
}
当我想在数据库中添加具有现有父公司的公司时,程序将转到else括号。现在我得到了一个错误,但这是确定的,它说我不能添加当前存在于数据库中的相同值
INSERT语句与外键约束冲突
“FK_dbo.Company_dbo.ParentCompany_ParentCategoryId”。冲突
发生在数据库中
“ASPNET_素食者_201504280012206_C73DB9E12D424DF80E198AC36A739CF”,表
“dbo.ParentCompany”,列“Id”。声明已终止
但是:)这就是我的观点。
我想将数据库中的现有字段添加到我的公司,但我不想将相同的父公司添加到数据库中
有什么想法可以实现我的观点吗?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);
}
公共操作结果创建([Bind(Include=“Id,Name,AnimalTesting,ParentCompany”)]公司)
{
List ExistCompany=(List)unitOfWork.CompanyRepository.List
(o=>o.Name==公司名称);
if(ModelState.IsValid&&ExistCompany.Count==0)
{
List Parents=(List)unitOfWork.ParentCompanyRepository.List
(o=>o.Name==company.ParentCompany.Name);
if(Parents.Count==0)
{
unitOfWork.ParentCompanyRepository.Add(company.ParentCompany);
unitOfWork.CompanyRepository.Add(公司);
}
其他的
{
foreach(父公司中的父公司p)
{
company.ParentCompany=p;
打破
}
无语境;
context=unitOfWork.GetDbContext();
context.Entry(company.ParentCompany).State=EntityState.Unchanged;
context.Entry(company.State=EntityState.Added;
SaveChanges();
}
返回操作(“索引”);
}
如果(ExistCompany.Count!=0)
ViewBag.ExistCompany=“具有该名称的公司已存在”;
返回视图(公司);
}
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);
}
公共操作结果创建([Bind(Include=“Id,Name,AnimalTesting,ParentCompany”)]公司)
{
List ExistCompany=(List)unitOfWork.CompanyRepository.List
(o=>o.Name==公司名称);
if(ModelState.IsValid&&ExistCompany.Count==0)
{
List Parents=(List)unitOfWork.ParentCompanyRepository.List
(o=>o.Name==company.ParentCompany.Name);
if(Parents.Count==0)
{
unitOfWork.ParentCompanyRepository.Add(company.ParentCompany);
unitOfWork.CompanyRepository.Add(公司);
}
其他的
{
foreach(父公司中的父公司p)
{
company.ParentCompany=p;
打破
}
无语境;
context=unitOfWork.GetDbContext();
context.Entry(company.ParentCompany).State=EntityState.Unchanged;
context.Entry(company.State=EntityState.Added;
SaveChanges();
}
返回操作(“索引”);
}
如果(ExistCompany.Count!=0)
ViewBag.ExistCompany=“具有该名称的公司已存在”;
返回视图(公司);
}