将空的新导航类带入剃刀页面页面模型 [英] Bring in empty new navigation class to razor pages page model

查看:81
本文介绍了将空的新导航类带入剃刀页面页面模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EF Core 2.1(使用Razor页面)



我有一个表格,模型和Razor Crud页面来创建新的考试时间表。



db表和模型有一个考试日期表和模型的外键ID,后者包含需要添加/更新考试时间表的关键日期。考试周期的各个阶段。所有这些日期都可以为空。



创建一个新的考试时间表时,唯一需要设置的字段是预定日期,只有这一个表中有7个日期。



我的问题分为两部分:



1)我如何创建一个空的(实例化但是with alls)ExaminationDates条目然后在创建考试时间表时将新的自动生成的主键附加到考试时间表外键?我认为当模型和导航属性正确完成时,LinQ sql会自动生成,所以我不想担心这个。



2)怎么做我在考试时间表中添加了预定日期日期拣货员?在这个预定日期我只需要一个。所有其他日期将在考试过程的各个阶段自动更新,我再次认为我可以轻松地使用LinQ。



我想知道在此预定日期的考试中具有导航属性。我也担心页面模型,我只是部分理解。使用单个查找下拉值很容易,但是使用包含整个日期负载的表/模型,这将如何工作?我已经编写C#多年,但我是EF Core和Entity Framework的新手。就像生活中常见的一样,我读了几百页,但似乎都没有复制我需要做的事情。



任何帮助都会受到赞赏。



我尝试了什么:



我试过阅读各种不同的页面。

EF Core 2.1 (using Razor Pages)

I have a table, model and Razor Crud Pages to create new Examination schedules.

The db table and model has a foreign key ID to an Examination Dates table and model, which in turn contains key dates that require adding/updating to an Exam schedule at various stages of the Exam cycle. All these Dates are nullable.

When creating a new Exam schedule, the only field which would (maybe) need to be set is the scheduled date, just this one of 7 dates in the table.

My question is in two parts:

1) How do I create an empty (instantiated but with all nulls) ExaminationDates entry and then attach it's new auto-generated primary key to the Examination Schedule foreign key when the Exam schedule is created? I presume the LinQ sql will be automatic when the models and navigation properties are done correctly, so I don't want to worry about that yet.

2) How do I add a Scheduled date datepicker to the Examination schedule? I only need one, for this scheduled date. All the other dates will be automatically updated at various stages of the course of an examination, which again I think I can do with LinQ easily enough.

I am wondering about having a navigation property on the Examination for this scheduled date. I'm also worrying about Page Models, which i only partially understand. It's easy to do crud with single lookup drop-down values but with a table / model containing a whole load of dates, how would this work? I've been coding C# for many years but I'm new to EF Core and Entity Framework. As is usual in life, I've read hundreds of pages but none seem to replicate what I need to do.

Any help would be appreciated.

What I have tried:

I've tried reading all sorts of different pages.

推荐答案

1)这不是特定于您的情况,因为您没有发布任何代码,因此我无法看到您的数据模型。但是,如果您使用EF,那么您不必担心主键外键值,因为这是实体框架的工作。您的DBContext将具有父类型记录的数据结构,其中包括子类型记录的列表。您在DBContext中创建父实体,将子实体添加到属于该父实体的列表中,并将DBContext告诉SaveChanges()

当然这个过程有变化,有一个异步版本,可取消的等等。但EF将负责从父记录中获取ID并将其包含在子记录中(假设您的DBContext数据结构正确)。



2)关于您的流程的信息不足以在此处提供帮助。



HTH,Mike
1) This isn't going to be specific to your situation because you're not posting any code so I can't see into your data model. But if you're using EF then you don't have to worry about the primary key foreign key values because that's entity framework's job. Your DBContext will have a data structure for the parent type records that includes a list of the child type records. You create the parent entity within the DBContext, you add in child entities to the the list belonging to that parent entity and you tell the DBContext to SaveChanges()
Naturally there are variations of this process, there's an Async version, one that's cancellable, etc. But the EF will take care of getting the ID from the parent record and including it with the child records (provided your DBContext data is structured correctly).

2) Not enough info about your process to be able to assist here.

HTH, Mike


这篇关于将空的新导航类带入剃刀页面页面模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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