【发布时间】:2017-08-23 13:05:19
【问题描述】:
为什么A和B是多对多的关系,在A表中插入记录时,B表中也会添加一条新记录?
有两个模型类,Customer 和 Course。一位客户可以选择许多课程。此外,许多客户可以选择一门课程。
我遇到的问题是,当我用他选择的一些课程(例如course1 和course2)创建客户时,course1 和course2 已经在Courses 表中,但它会再次自动插入course1 和course2,这不是我想要的。
客户类:
public class Customer
{
public int Id { get; set; }
[MaxLength(20)]
public string City { get; set; }
[Range(16,58)]
public int Age { get; set; }
public Gender Gender { get; set; }
[MaxLength(20)]
public string Name { get; set; }
public IList<Course> CoursesEnrolled { get; set; }
}
课程类:
public class Course
{
public int Id { get; set; }
[MaxLength(50)]
[Required]
public string Name { get; set; }
}
CustomerController, Save 方法:
[HttpPost]
public ActionResult Save( Customer customer
{
if (customer.Id == 0)
{
Customer customerInDb = new Customer()
{
Name = customer.Name ,
City = customer.City ,
Age = customer.Age ,
Gender = customer.Gender ,
CoursesEnrolled = customer.CoursesEnrolled
};
_context.Customers.Add(customerInDb);
}
else
{
var customerInDb = _context.Customers.SingleOrDefault(c => c.Id == customer.Id);
if (customerInDb == null)
{
return HttpNotFound();
}
customerInDb.Name = customer.Name;
customerInDb.Age = customer.Age;
customerInDb.City = customer.City;
customerInDb.Gender = customer.Gender;
customerInDb.CoursesEnrolled = customer.CoursesEnrolled;
}
try
{
_context.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
Exception raise = dbEx;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
string message = string.Format("{0}:{1}",
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
raise = new InvalidOperationException(message, raise);
}
}
throw raise;
}
return RedirectToAction("Index" , "Customer");
}
Courses 表中有四门课程:
当我创建一个选择了一些课程的客户时,Customer 保存成功,CustomersCourses 也添加了数据。
但在Courses表中,客户注册的选择课程也被添加到课程表中。
红色三角形中的数据不是我们想要的。
任何帮助或建议将不胜感激,谢谢!
【问题讨论】:
-
我想这完全取决于您在
customer.CoursesEnrolled中拥有的内容以及您如何填写这些内容。如果那里没有现有的课程 ID,它们当然是新课程。但你没有向我们展示这些信息,所以我们不得不猜测...... -
感谢您的回复。原因就像ashin说的,他给出了两个解决方案。
标签: c# sql entity-framework many-to-many