通过MVC将数据写入数据库的最佳方法是什么? [英] What is the best way to write data via MVC into database?

查看:410
本文介绍了通过MVC将数据写入数据库的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有EF Core的MVC进行家庭作业项目. 我正在寻找将数据写入数据库的最佳方法. (我是初学者) 有两个表. Predbilježba(注册)和Seminari(研讨会)

I am working on a homework project using MVC with EF Core. I am looking for the best way to write data into the database. (I am beginner) There are two tables. Predbilježba(Enrollment) and Seminari(Seminars)

public class Predbilježba
{
    [Key]
    public int PredbilježbeID { get; set; }
    public string Ime { get; set; }
    public string Prezime { get; set; }
    public string Adresa { get; set; }
    public string Email { get; set; }
    public string Telefon { get; set; }
    public bool Status { get; set; }

    [DataType(DataType.Date)]
    public DateTime DatumUpisa { get; set; }  
    public int SeminarID { get; set; }
    public Seminar Seminar { get; set; }
}

public class Seminar
{
    public int SeminarID { get; set; }
    public string Naziv { get; set; }
    public string Opis { get; set; }

    [DataType(DataType.Date)]
    public DateTime Datum { get; set; }
    public bool Popunjen { get; set; }
    public ICollection<Predbilježba> Predbilježba { get; set; }
}

我需要在数据库中插入某种注册(名称:Predbilježba). 注册已连接到名为Seminars(名称为Seminari)的表.

I need to insert a sort of Enrollment (Named: Predbilježba) into the database. Enrollment is connected to a table called Seminars (Named: Seminari).

因此,当某人注册"到研讨会"时,他/她需要将基本数据插入表格(姓名,电话号码等)中,并且他们需要从给定的列表中选择研讨会" 研讨会"表中的研讨会.

So when a person is "enrolling" into a "seminar", he/she needs to insert basic data into form (name, phone number, etc.) and they need to choose a "seminar" from a list of given seminars which are in "Seminar" table.

因此,当他们单击保存"时,他们的基本数据将与选择的研讨会"一起写入Predbilježba/(英文).

So when they click "Save", their basic data is written into Predbilježba / (eng. Enrollment)" along with chosen "seminar"

所以我已经有这两个模型的控制器,以及用于创建,编辑等的适当视图.

So I already have controllers for these 2 models, and appropriate views to create, edit, and so on..

我的问题是:我是否创建一个单独的控制器/模型/视图以将数据插入表中?有人可以给我一些例子吗?

My question is: Do I create a separate controller/model/view to insert data into tables? Can someone give me some example of how it is done?

为进一步说明,我需要制作一个用户端页面,用户可以在其中写名称,姓氏等并选择所需的研讨会来注册"到研讨会".到目前为止,我具有功能数据库,身份(将在项目稍后使用),两个模型的控制器以及适当的视图,可以在其中编辑Prebilježbe(中文注册)和研讨会.

To clarify further, I need to make user side page where user can "enroll" to "seminar" by writing name, last name, etc.. and by choosing the desired seminar. For now I have functional database, Identity (which will be used later in project), controllers of both models, and appropriate views where I can edit Prebilježbe(eng. Enrollments) and Seminars.

页面图片如下:

Images of page follow:

因此,如图3所示,当用户单击Upišise(注册)时,需要将选定的研讨会以及在单击后打开的基本信息(图4)写入数据库Predbilježbe"(英语)报名人数)

So when user clicks Upiši se (eng. enroll) as shown in image number 3. , that selected Seminar, along with basic info that opens after the click (image 4 ) needs to be written into database "Predbilježbe" (eng Enrollments)

此"Upis"页面将是用户输入页面,"Seminari"和Predbilježbe"将是管理页面.

This "Upis" page would be a user input page, and "Seminari" and "Predbilježbe" would be admin pages..

推荐答案

如果我正确理解了您的问题,那么您在问的是好的建筑设计.不是吗(如果没有,请让我知道编辑答案).

If I understand your question correctly, you are asking about good architectural design. Aren't you? (if not please let me know to edit the answer).

您有许多架构选择和可能性.最开始的一个是服务库体系结构模式.我在这里省略了Repository一词,因为EF已经(至少在某种程度上)已经是Repository模式的实现.

You have many architectural choices and possibilities. The easiest one for you to start with is the Service-Repository architectural pattern. I would omit the Repository word here because EF is already (in my opinion) a Repository pattern implementation (at least partially).

为简单起见,您想从服务体系结构模式开始.这是关于创建一个类的,该类将DbContext注入其构造中(让我们将其命名为PredbilježbaService).在此类中,您将处理逻辑的所有操作(包括数据库EF查询).

So to keep it simple, you would like to start with Service architectural pattern. Which is about creating a class, which injects the DbContext in its construction (let's name it PredbilježbaService). And in this class, you handle all operations of your logic (including database EF queries).

然后将此类注入到控制器中,并从控制器中的该服务类(处理数据库)中调用所需的函数.

Then you inject this class to your controller and call the required functions from that service class (which deals with the database) in your controller.

同一过程可以应用于其他实体Seminar.

The same process can be applied to the other entity Seminar.

P.S.注入是指使用任何IoC设计模式(在ASP.Net Core中,依赖项注入已内置).

P.S. by injecting I mean using any IoC design pattern (in ASP.Net Core, dependency injection is already built-in).

因此,在简短地说完后,直接回答您的问题,一个好的软件设计是通过创建一个单独的类来处理数据库操作(添加行,编辑行等)

So after these brief words, to answer your question directly, yes, a good software design would be by creating a separate class which handles database operations (adding rows, editing rows, etc.)

这篇关于通过MVC将数据写入数据库的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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