实体框架使用视图和存储过程

时间:2021-07-21 02:08:16

I have the following basic table structure (that I'm not allowed to change) in my database:

我的数据库中有以下基本表结构(我不允许更改):

Address
(PK) AddressId
Line1
...

地址(PK)AddressId Line1 ...

Person
(PK) PersonId
(FK) AddressId
FirstName
...

人(PK)PersonId(FK)AddressId FirstName ...

Student
(PK) StudentId
(FK) PersonId
StudentNumber
...

学生(PK)StudentId(FK)PersonId StudentNumber ...

I would like to combine Person and Student into a Student entity using the Table-per-Type approach. However, these tables don't conform to EFs restriction that the sub-table (Student) must have the PK and FK be the same column. Unlike in my db, which they are seperate columns.

我想使用Table-per-Type方法将Person和Student组合成Student实体。但是,这些表不符合EFs限制,即子表(Student)必须具有PK和FK是同一列。与我的数据库不同,它们是单独的列。

My next idea was to create a view and create an entity off the view. I create a view called StudentView with a join and all the columns. I then imported this view into the EF model. All was fine until I tried to update. Since views can't update, I created Insert/Update/Delete stored procedures and bound them into my Student entity (I did the extra sp paramaters for all FK relationships).

我的下一个想法是创建一个视图并在视图外创建一个实体。我创建了一个名为StudentView的视图,其中包含连接和所有列。然后我将此视图导入EF模型。一切都很好,直到我试图更新。由于视图无法更新,我创建了插入/更新/删除存储过程并将它们绑定到我的学生实体(我为所有FK关系做了额外的sp参数)。

However, now I'm getting the following error:

但是,现在我收到以下错误:

Error 2027: If an EntitySet or AssociationSet includes a function mapping, all related entity and AssociationSets in the EntityContainer must also define function mappings.

错误2027:如果EntitySet或AssociationSet包含函数映射,则EntityContainer中的所有相关实体和AssociationSets也必须定义函数映射。

I haven't been able to find much info anywhere about this. It seems to me that it means that if one entity uses stored procedures, all entities do (or at least all entities that are referenced, which in my case equates to roughly all). However, other error messages I've received from EF have been deceiving.

我无法在任何地方找到这方面的信息。在我看来,这意味着如果一个实体使用存储过程,所有实体都会(或至少所有被引用的实体,在我的情况下等于大致全部)。但是,我从EF收到的其他错误消息一直在欺骗。

Does anyone know how to get past this? Also, any suggestions on how to use Table-per-Type in my case (which I don't think is possible) would be helpful too.

有谁知道如何通过这个?另外,关于如何在我的案例中使用Table-per-Type的任何建议(我认为不可能)也会有所帮助。

1 个解决方案

#1


I figured this out. It turns out it was because I had set some relationships to be 0..1:1. It seems when this happens, anything with that relationship type must use stored procedures too. Setting the relationships back to 0..1:* fixed my problem.

我想出来了。事实证明这是因为我设置了一些关系为0..1:1。看来,当发生这种情况时,任何具有该关系类型的东西也必须使用存储过程。将关系设置回0..1:*解决了我的问题。

#1


I figured this out. It turns out it was because I had set some relationships to be 0..1:1. It seems when this happens, anything with that relationship type must use stored procedures too. Setting the relationships back to 0..1:* fixed my problem.

我想出来了。事实证明这是因为我设置了一些关系为0..1:1。看来,当发生这种情况时,任何具有该关系类型的东西也必须使用存储过程。将关系设置回0..1:*解决了我的问题。