Coud不保存多对多代码第一个数据库? [英] Coudn't save many-to-many code first db ?

查看:92
本文介绍了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

推荐答案

这是因为,你试图以不正确的顺序添加记录中的值

想想,你在DB中有不同的顺序,而且与参数不匹配您通过此代码的类型
This is because, you are trying to add the value in the record in incorrect order
think, you have different order in DB and that is not matching with the parameter type which you are passing through this code


这篇关于Coud不保存多对多代码第一个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆