如何通过EF存储库将记录添加到MVC中的多个表中



我有三个类似于下面的型号第一个学生模型

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" />

现在我想要在数据库中添加学生时,我想将其他数据保存到学生表中,然后将课程信息保存到StudentClass表中。我该怎么做???预先感谢

这更多的是设计模式的问题和您的工作方式。根据您的需求,保存应通过服务来处理,让我们致电您的sustementService.cs

这将同时使用efclassrepository&amp;efstudentrepository

服务接口istudentservice.cs应由您保存的页面的控制器使用,这应该在服务上保存,这又将接收您从页面传递的任何对象,并使用这些详细信息来调用保存两个存储库。因此,作为一个快速示例:

public class StudentService : IStudentService
    {
        private readonly IStudentRepository studentRepository;
        private readonly IClassRepository classRepository;
        public StudentService(IStudentRepository studentRepository, IClassRepository classRepository)
        {
            if (studentRepository == null) throw new ArgumentNullException(nameof(studentRepository));
            if (classRepository == null) throw new ArgumentNullException(nameof(classRepository));
            this.studentRepository = studentRepository;
            this.classRepository = classRepository;
        }
        public void Save(Student student, Class studentsClass)
        {
            if (student == null) throw new ArgumentNullException(nameof(student));
            if (studentsClass == null) throw new ArgumentNullException(nameof(studentsClass));
            studentRepository.Save(student);
            classRepository.Save(studentsClass);
        }
    }

接口:

interface IStudentService
    {
        void Save(Student student, Class studentsClass);
    }

假设您不想为每个学生保存一个空白课,而没有信息:

public class StudentPageModel
    {
        public int SchoolYear { get; private set; }
        public Student Student { get; private set; }
        public Class Class { get; private set; }
        public StudentPageModel(int schoolYear, Student student, Class studentClass)
        {
            SchoolYear = schoolYear;
            Student = student;
            Class = studentClass;
        }
    }

当然,您可以在控制器中的两个存储库中介绍并删除服务,但是它的可重复使用性,您应该真正将其推向服务来处理,需要将控制器视为页面背后的代码并尽可能最小和愚蠢,将思想留给服务,演示者等。

养成使您的课程不可变的习惯,如上所述,应该在构造函数和方法中设置字段和属性,而不是直接访问,有助于构建某些代码完整性并防止意外变化。

相关内容

  • 没有找到相关文章

最新更新