我有三个模型,如下面的第一个学生模型
public class Student
{
public int ID { get; set; }
[ForeignKey("account")]
public int AccountID { get; set; }
public Account account{ get; set; }
//some more
}
二等车型
public class ClassMigration
{
public int ID { get; set; }
public String Name { get; set; }
public bool A { get; set; }
//etc
}
第三个是学生班
public class StudentClass
{
public int ID { get; set; }
[ForeignKey("student")]
public int StudentID { get; set; }
public Student student { get; set; }
[ForeignKey("clas")]
public int ClassID { get; set; }
public ClassMigration clas { get; set; }
public String Section { get; set; }
public String Session { get; set; }
}
这是我的学生和班级的抽象课程
namespace Domain.Abstract
{
public interface IStudent
{
IEnumerable<Student> Students { get; }
Student SaveStudent(Student student);
Student DeleteStudent(int ID);
}
}
namespace Domain.Abstract
{
public interface IClassMigration
{
IEnumerable<ClassMigration> Classes{ get; }
ClassMigration SaveClass(ClassMigration clas);
ClassMigration DeleteClass(int ID);
}
}
这两个类的存储库是:
namespace Domain.Concret
{
public class EFStudentRepository:IStudent
{
public readonly DbAccess context = new DbAccess();
public IEnumerable<Entities.Student> Students
{
get { return context.Students; }
}
public Student SaveStudent(Entities.Student student)
{
if (student.ID == 0)
{
Account account = new Account();
account.UserName = student.RegistrationNo;
account.Password = "123456";
account.Role = UserRole.Student;
context.Accounts.Add(account);
context.SaveChanges();
student.AccountID = context.Accounts.Max(m => m.ID);
context.Students.Add(student);
}
else
{
var org = context.Students.Find(student.ID);
if (org != null)
{
context.Entry(org).CurrentValues.SetValues(student);
}
}
context.SaveChanges();
if(student.ID==0)
{
student.ID = context.Students.Max(m => m.ID);
}
return student;
}
public Entities.Student DeleteStudent(int ID)
{
var std = context.Students.Find(ID);
if (std != null)
{
context.Students.Remove(std);
context.SaveChanges();
}
return std;
}
}
}
namespace Domain.Concret
{
public class EFClassRepository:IClassMigration
{
public DbAccess context = new DbAccess();
public IEnumerable<Entities.ClassMigration> Classes
{
get { return context.Classes; }
}
public Entities.ClassMigration SaveClass(Entities.ClassMigration clas)
{
if (clas.ID == 0)
{
context.Classes.Add(clas);
}
else
{
var org = context.Classes.Find(clas.ID);
if (org != null)
{
context.Entry(org).CurrentValues.SetValues(clas);
}
}
context.SaveChanges();
if (clas.ID == 0)
{
clas.ID = context.Classes.Max(m => m.ID);
}
return clas;
}
public Entities.ClassMigration DeleteClass(int ID)
{
var clas = context.Classes.Find(ID);
if (clas != null)
{
context.Classes.Remove(clas);
context.SaveChanges();
}
return clas;
}
}
}
以及我对班级和部分的看法
<div class="form-group">
<label for="Class">Class</label>
@Html.DropDownList("Class", @ViewBag.Classes as SelectList, new { @class = "form-control" })
</div>
<div class="form-group">
<label for="Section">Select Section</label>
<select id="Section" class="form-control">
<option selected="selected" value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
</select>
</div>
<div class="form-group">
<label for="FatherName">Father Name</label>
<input type="text" class="form-control" id="FatherName" placeholder="Father Name" />
现在我想当我们将学生添加到数据库时,我想将其他数据保存到 Student 表,并将班级信息保存到 StudentClass 表。我该怎么做???提前致谢