本文介绍了Coud不保存多对多代码第一个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I'm using code first and fluent api this is my models
public Admin()
{
this.course = new HashSet<Courses>();
}
[Key]
public int ID { get; set; }
public string LoginName { get; set; }
public virtual ICollection<Courses> course { get; set; }
}
public Courses()
{
this.admin = new HashSet<Admin>();
}
[Key]
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Admin> admin { get; set; }
}
and this is fluent api
modelBuilder.Entity<Admin>()
.HasMany(e => e.course)
.WithMany(e => e.admin);
and this is my controller which I'm getting the courseid from cshml
public ActionResult Admins( Admin rec, IList CourseId) {
foreach (var item in CourseId)
{
var cr = new Courses();
cr.ID = item;
rec.course.Add(cr);
}
db.Admins.Add(rec);
db.SaveChanges();
it gives me error when I'm trying to save db.SaveChanges()
could any one tell me what's the wrong here ?
我尝试了什么:
我不知道应该尝试什么
What I have tried:
I don't know what should I try
推荐答案
这篇关于Coud不保存多对多代码第一个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!