无法将视图添加到 EF 数据模型
我有一个正在尝试添加到我的 ADO.NET 实体数据模型中的视图。每次我尝试从数据库更新并检查视图时,它都会刷新其他所有内容,但不会添加视图。
我没有收到错误消息或输出,所以我不知道视图出了什么问题。其他观点没有问题。我错过了什么吗?有办法打开错误消息吗? Visual Studio 2008 sp1
更新:我找到了此链接,但这些解决方案并未解决问题。 MSDN 论坛
更新:我无法添加的视图将从另一个视图中查询。
更新:帮助
WITH cte AS (SELECT dbo.TBL_Gharardad.PK_Shenase, dbo.TBL_Gharardad.FK_NoeKhedmat AS NoeKhedmatId,
dbo.TBL_NoeKhedmat.NoeKhedmat AS [نوع خدمت], dbo.TBL_Gharardad.OnvaneKhedmat AS [عنوان خدمت],
dbo.TBL_Gharardad.MahaleEraeieKhedmat AS [محل ارائه خدمت],
dbo.TBL_Gharardad.FK_NahveieTaieeneBarande AS NahveieTaeeneBararndeId,
dbo.TBL_NahveieTaieeneBarande.NahveieTaieeneBarande AS [نحوه تعيين برنده],
dbo.TBL_Gharardad.TarikheShorooeGharardad_Jalali AS [تاريخ شروع قرارداد],
dbo.TBL_Gharardad.TarikhePayaneGharardad_Jalali AS [تاريخ پايان قرارداد], dbo.TBL_Gharardad.FK_VahedeArz AS VahedeArzId,
dbo.TBL_VahedeArz.VahedeArz AS [واحد ارز], dbo.TBL_Gharardad.MablagheDariaftiKol AS [مبلغ دريافتي کل],
dbo.TBL_Gharardad.MablaghePardakhtieKol AS [مبلغ پرداختي کل], dbo.TBL_Gharardad.SahmeKarfarma AS [درصد مشارکت کارفرما],
100 - dbo.TBL_Gharardad.SahmeKarfarma AS [درصد مشارکت پيمانکار], dbo.TBL_Gharardad.TedadNirooyeMard AS [تعداد نيروي مرد],
dbo.TBL_Gharardad.TedadNirooyeZan AS [تعداد نيروي زن],
dbo.TBL_Gharardad.TedadNirooyeMard + dbo.TBL_Gharardad.TedadNirooyeZan AS [تعداد کل نيروها],
dbo.TBL_Gharardad.FK_TarafeGharardad AS TarafeGharardadId,
CASE TBL_TarafeGharardad.Hoghooghi WHEN 0 THEN ISNULL(TBL_TarafeGharardad.Naam, ' ')
+ ' ' + ISNULL(TBL_TarafeGharardad.NaameKhanevadegi, ' ') ELSE TBL_TarafeGharardad.NameSherkat END AS [طرف قرارداد],
dbo.TBL_Gharardad.FK_VahedeVagozarKonande AS VahedeVagozarKonandeId,
dbo.TBL_VahedeVagozarKonande.VahedeVagozarKonande AS [واحد واگذار کننده], dbo.TBL_Gharardad.ShomareGharardad AS [شماره قرارداد],
dbo.TBL_Gharardad.TarikheGharardad_Jalali AS [تاريخ قرارداد],
CASE VaziateGharardad WHEN 0 THEN N'لغو شده' WHEN 1 THEN N'ثبت اوليه' WHEN 2 THEN N'فسخ' WHEN 3 THEN N'ثبت نهايي ' WHEN 4 THEN
N' جاري ' WHEN 5 THEN N'تمام شده ' WHEN 6 THEN N' متمم ' END AS [وضعيت قرارداد], dbo.TBL_NoeMoamele.NoeMoamele AS [نوع معامله]
FROM dbo.TBL_Gharardad INNER JOIN
dbo.TBL_NoeKhedmat ON dbo.TBL_Gharardad.FK_NoeKhedmat = dbo.TBL_NoeKhedmat.PK_Id INNER JOIN
dbo.TBL_NahveieTaieeneBarande ON
dbo.TBL_Gharardad.FK_NahveieTaieeneBarande = dbo.TBL_NahveieTaieeneBarande.PK_Id INNER JOIN
dbo.TBL_VahedeArz ON dbo.TBL_Gharardad.FK_VahedeArz = dbo.TBL_VahedeArz.PK_Id INNER JOIN
dbo.TBL_TarafeGharardad ON dbo.TBL_Gharardad.FK_TarafeGharardad = dbo.TBL_TarafeGharardad.PK_Id INNER JOIN
dbo.TBL_VahedeVagozarKonande ON
dbo.TBL_Gharardad.FK_VahedeVagozarKonande = dbo.TBL_VahedeVagozarKonande.PK_Id INNER JOIN
dbo.TBL_NoeMoamele ON dbo.TBL_Gharardad.FK_NoeMoamele = dbo.TBL_NoeMoamele.PK_Id)
SELECT v_Gharardad.شناسه, v_Gharardad.NoeKhedmatId, v_Gharardad.[نوع خدمت], v_Gharardad.[عنوان خدمت], v_Gharardad.[محل ارائه خدمت],
v_Gharardad.NahveieTaeeneBararndeId, v_Gharardad.[نحوه تعيين برنده], v_Gharardad.[تاريخ شروع قرارداد], v_Gharardad.[تاريخ پايان قرارداد],
v_Gharardad.VahedeArzId, v_Gharardad.[واحد ارز], v_Gharardad.[مبلغ دريافتي کل], v_Gharardad.[مبلغ پرداختي کل], v_Gharardad.[درصد مشارکت کارفرما],
v_Gharardad.[درصد مشارکت پيمانکار], v_Gharardad.[تعداد نيروي مرد], v_Gharardad.[تعداد نيروي زن], v_Gharardad.[تعداد کل نيروها],
v_Gharardad.TarafeGharardadId, v_Gharardad.[طرف قرارداد], v_Gharardad.VahedeVagozarKonandeId, v_Gharardad.[واحد واگذار کننده],
v_Gharardad.[شماره قرارداد], v_Gharardad.[تاريخ قرارداد], v_Gharardad.[وضعيت قرارداد], v_Gharardad.[نوع معامله]
FROM dbo.TBL_Gharardad AS TBL_Gharardad_3 INNER JOIN
dbo.v_GharardadRecords AS v_Gharardad ON v_Gharardad.شناسه = TBL_Gharardad_3.PK_Shenase
WHERE (TBL_Gharardad_3.FK_GharardadeAsli IS NULL) AND (TBL_Gharardad_3.PK_Shenase NOT IN
(SELECT FK_GharardadeAsli
FROM dbo.TBL_Gharardad AS TBL_Gharardad_2
WHERE (FK_GharardadeAsli IS NOT NULL)))
UNION
SELECT sub.FK_GharardadeAsli AS شناسه, cte_2.NoeKhedmatId, cte_2.[نوع خدمت], cte_2.[عنوان خدمت], cte_2.[محل ارائه خدمت], cte_2.NahveieTaeeneBararndeId,
cte_2.[نحوه تعيين برنده], cte_2.[تاريخ شروع قرارداد], cte_2.[تاريخ پايان قرارداد], cte_2.VahedeArzId, cte_2.[واحد ارز], cte_2.[مبلغ دريافتي کل], cte_2.[مبلغ پرداختي کل],
cte_2.[درصد مشارکت کارفرما], cte_2.[درصد مشارکت پيمانکار], cte_2.[تعداد نيروي مرد], cte_2.[تعداد نيروي زن], cte_2.[تعداد کل نيروها], cte_2.TarafeGharardadId,
cte_2.[طرف قرارداد], cte_2.VahedeVagozarKonandeId, cte_2.[واحد واگذار کننده], cte_2.[شماره قرارداد], cte_2.[تاريخ قرارداد], cte_2.[وضعيت قرارداد],
cte_2.[نوع معامله]
FROM dbo.v_GharardadRecords AS cte_2 INNER JOIN
(SELECT FK_GharardadeAsli, MAX(PK_Shenase) AS PK_Shenase, MAX(TarikheSabt) AS TarikheSabt
FROM dbo.TBL_Gharardad AS TBL_Gharardad_1
WHERE (FK_GharardadeAsli IS NOT NULL)
GROUP BY FK_GharardadeAsli) AS sub ON sub.PK_Shenase = cte_2.شناسه
I have a view that I am trying to add to my ADO.NET Entity Data Model. Every time I try to Update From Database, and check the view, it refreshes everything else, but does not add the view.
I get no error message or output, so I have no idea what is wrong with the view. Other views are no problem. Am I missing something, is there a way to turn error messages on?
Visual Studio 2008 sp1
Update: I found this link but the problem didn't solve with these solutions.
MSDN Forum
Update: The view that i can't add it will query from another view.
Update: Help
WITH cte AS (SELECT dbo.TBL_Gharardad.PK_Shenase, dbo.TBL_Gharardad.FK_NoeKhedmat AS NoeKhedmatId,
dbo.TBL_NoeKhedmat.NoeKhedmat AS [نوع خدمت], dbo.TBL_Gharardad.OnvaneKhedmat AS [عنوان خدمت],
dbo.TBL_Gharardad.MahaleEraeieKhedmat AS [محل ارائه خدمت],
dbo.TBL_Gharardad.FK_NahveieTaieeneBarande AS NahveieTaeeneBararndeId,
dbo.TBL_NahveieTaieeneBarande.NahveieTaieeneBarande AS [نحوه تعيين برنده],
dbo.TBL_Gharardad.TarikheShorooeGharardad_Jalali AS [تاريخ شروع قرارداد],
dbo.TBL_Gharardad.TarikhePayaneGharardad_Jalali AS [تاريخ پايان قرارداد], dbo.TBL_Gharardad.FK_VahedeArz AS VahedeArzId,
dbo.TBL_VahedeArz.VahedeArz AS [واحد ارز], dbo.TBL_Gharardad.MablagheDariaftiKol AS [مبلغ دريافتي کل],
dbo.TBL_Gharardad.MablaghePardakhtieKol AS [مبلغ پرداختي کل], dbo.TBL_Gharardad.SahmeKarfarma AS [درصد مشارکت کارفرما],
100 - dbo.TBL_Gharardad.SahmeKarfarma AS [درصد مشارکت پيمانکار], dbo.TBL_Gharardad.TedadNirooyeMard AS [تعداد نيروي مرد],
dbo.TBL_Gharardad.TedadNirooyeZan AS [تعداد نيروي زن],
dbo.TBL_Gharardad.TedadNirooyeMard + dbo.TBL_Gharardad.TedadNirooyeZan AS [تعداد کل نيروها],
dbo.TBL_Gharardad.FK_TarafeGharardad AS TarafeGharardadId,
CASE TBL_TarafeGharardad.Hoghooghi WHEN 0 THEN ISNULL(TBL_TarafeGharardad.Naam, ' ')
+ ' ' + ISNULL(TBL_TarafeGharardad.NaameKhanevadegi, ' ') ELSE TBL_TarafeGharardad.NameSherkat END AS [طرف قرارداد],
dbo.TBL_Gharardad.FK_VahedeVagozarKonande AS VahedeVagozarKonandeId,
dbo.TBL_VahedeVagozarKonande.VahedeVagozarKonande AS [واحد واگذار کننده], dbo.TBL_Gharardad.ShomareGharardad AS [شماره قرارداد],
dbo.TBL_Gharardad.TarikheGharardad_Jalali AS [تاريخ قرارداد],
CASE VaziateGharardad WHEN 0 THEN N'لغو شده' WHEN 1 THEN N'ثبت اوليه' WHEN 2 THEN N'فسخ' WHEN 3 THEN N'ثبت نهايي ' WHEN 4 THEN
N' جاري ' WHEN 5 THEN N'تمام شده ' WHEN 6 THEN N' متمم ' END AS [وضعيت قرارداد], dbo.TBL_NoeMoamele.NoeMoamele AS [نوع معامله]
FROM dbo.TBL_Gharardad INNER JOIN
dbo.TBL_NoeKhedmat ON dbo.TBL_Gharardad.FK_NoeKhedmat = dbo.TBL_NoeKhedmat.PK_Id INNER JOIN
dbo.TBL_NahveieTaieeneBarande ON
dbo.TBL_Gharardad.FK_NahveieTaieeneBarande = dbo.TBL_NahveieTaieeneBarande.PK_Id INNER JOIN
dbo.TBL_VahedeArz ON dbo.TBL_Gharardad.FK_VahedeArz = dbo.TBL_VahedeArz.PK_Id INNER JOIN
dbo.TBL_TarafeGharardad ON dbo.TBL_Gharardad.FK_TarafeGharardad = dbo.TBL_TarafeGharardad.PK_Id INNER JOIN
dbo.TBL_VahedeVagozarKonande ON
dbo.TBL_Gharardad.FK_VahedeVagozarKonande = dbo.TBL_VahedeVagozarKonande.PK_Id INNER JOIN
dbo.TBL_NoeMoamele ON dbo.TBL_Gharardad.FK_NoeMoamele = dbo.TBL_NoeMoamele.PK_Id)
SELECT v_Gharardad.شناسه, v_Gharardad.NoeKhedmatId, v_Gharardad.[نوع خدمت], v_Gharardad.[عنوان خدمت], v_Gharardad.[محل ارائه خدمت],
v_Gharardad.NahveieTaeeneBararndeId, v_Gharardad.[نحوه تعيين برنده], v_Gharardad.[تاريخ شروع قرارداد], v_Gharardad.[تاريخ پايان قرارداد],
v_Gharardad.VahedeArzId, v_Gharardad.[واحد ارز], v_Gharardad.[مبلغ دريافتي کل], v_Gharardad.[مبلغ پرداختي کل], v_Gharardad.[درصد مشارکت کارفرما],
v_Gharardad.[درصد مشارکت پيمانکار], v_Gharardad.[تعداد نيروي مرد], v_Gharardad.[تعداد نيروي زن], v_Gharardad.[تعداد کل نيروها],
v_Gharardad.TarafeGharardadId, v_Gharardad.[طرف قرارداد], v_Gharardad.VahedeVagozarKonandeId, v_Gharardad.[واحد واگذار کننده],
v_Gharardad.[شماره قرارداد], v_Gharardad.[تاريخ قرارداد], v_Gharardad.[وضعيت قرارداد], v_Gharardad.[نوع معامله]
FROM dbo.TBL_Gharardad AS TBL_Gharardad_3 INNER JOIN
dbo.v_GharardadRecords AS v_Gharardad ON v_Gharardad.شناسه = TBL_Gharardad_3.PK_Shenase
WHERE (TBL_Gharardad_3.FK_GharardadeAsli IS NULL) AND (TBL_Gharardad_3.PK_Shenase NOT IN
(SELECT FK_GharardadeAsli
FROM dbo.TBL_Gharardad AS TBL_Gharardad_2
WHERE (FK_GharardadeAsli IS NOT NULL)))
UNION
SELECT sub.FK_GharardadeAsli AS شناسه, cte_2.NoeKhedmatId, cte_2.[نوع خدمت], cte_2.[عنوان خدمت], cte_2.[محل ارائه خدمت], cte_2.NahveieTaeeneBararndeId,
cte_2.[نحوه تعيين برنده], cte_2.[تاريخ شروع قرارداد], cte_2.[تاريخ پايان قرارداد], cte_2.VahedeArzId, cte_2.[واحد ارز], cte_2.[مبلغ دريافتي کل], cte_2.[مبلغ پرداختي کل],
cte_2.[درصد مشارکت کارفرما], cte_2.[درصد مشارکت پيمانکار], cte_2.[تعداد نيروي مرد], cte_2.[تعداد نيروي زن], cte_2.[تعداد کل نيروها], cte_2.TarafeGharardadId,
cte_2.[طرف قرارداد], cte_2.VahedeVagozarKonandeId, cte_2.[واحد واگذار کننده], cte_2.[شماره قرارداد], cte_2.[تاريخ قرارداد], cte_2.[وضعيت قرارداد],
cte_2.[نوع معامله]
FROM dbo.v_GharardadRecords AS cte_2 INNER JOIN
(SELECT FK_GharardadeAsli, MAX(PK_Shenase) AS PK_Shenase, MAX(TarikheSabt) AS TarikheSabt
FROM dbo.TBL_Gharardad AS TBL_Gharardad_1
WHERE (FK_GharardadeAsli IS NOT NULL)
GROUP BY FK_GharardadeAsli) AS sub ON sub.PK_Shenase = cte_2.شناسه
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
为了能够将视图添加到模型中,实体框架需要数据库视图中至少有一列不可为空。
To be able to add a view into a model, Entity Framework needs at least one column in database view to be not nullable.
检查您的视图是否确实是 *.edmx 文件中的视图。
正确:
错误:
Check if your View is for sure View in *.edmx file.
Correct:
Wrong:
您可以向视图添加行计数,并使其看起来像不为空的关键“Id”字段。一个例子:-
You can add a rowcount to the view and make it look like a key "Id" field that is not null. An example:-
您在视图中添加了主键以外的字段,实体是坏男孩
You add the field than primary key in your view, entity is bad boy with it
我试图加载具有单个可为空列的视图。我的解决方案是将可为空的列包装在 isnull 中并提供默认值零:
I was trying to load a view with a single nullable column. My solution for this was to wrap the nullable column in isnull and provide a default of zero:
这可能是有关该主题的答案的组合,但我读过的所有内容似乎都没有提到这个特定的修复。
我的视图连接了三个表,并且我收到了如上所述的错误。我从两个表中选择了 PK,但没有从第三个表中选择。包含第三个表中的 PK 后,实体框架就满意了。
This might be a combination of answers on this topic but all that I've read doesn't seem to mention this particular fix.
My view joins three tables and I was getting errors as mentioned. I had SELECT'd the PK from two of the tables but not the third. After including the PK from the third table, entity framework was satisfied.
在视图中添加 id 列:
之后,您可以在实体模型中添加视图。
Add
id
column in your view:After that, you can add your view in the Entity Model.
添加一个别名为“id”的虚拟字段,该字段不为空
Add a dummy field with an alias "id", which is not null
只用了这个,其他都没用。我已经经历过几次了,主键 ID 技巧就做到了。这次不是。
绝望中,我将它们重写为存储过程,但它们仍然没有出现!
原因最终是 onedrive 在两台计算机之间同步 sln 文件夹。我删除了.vs、bin和obj文件夹并重新编译,终于成功了。
Just had this and nothing else worked. I've had it a few times and the primary key ID trick did it. Not this time.
In desperation I rewrote them as stored procs and STILL they didn't come up!
The cause ended up being onedrive synching the sln folder between two machines. I deleted the .vs, bin and obj folders and recompiled, and it finally worked.
如果您的视图不包含主键列,则它不会添加到 edmx 文件。
If your view does not contain the primary key column,then it wont add to edmx file.
当我尝试添加不从另一个表中选择主键的视图时,我也遇到了同样的行为。 (就像 Ladislav Mrnka 评论的那样)
我解决这个问题的策略是将视图减少到尽可能简单(1 列)并尝试添加它。将其添加到模型后,慢慢引入更多列并刷新模型以确保视图仍然存在。您通常可以确定视图的哪个部分出现 EDM 问题。
I have experienced this same behaviour when I try to add a view that doesn't select a primary key from another table. (Like Ladislav Mrnka has commented)
My strategy for solving this is to reduce the view to as simple as possible (1 column) and try and to get it added. Once you have it added to the model, slowly bring in more columns and refresh the model to make sure the view is still there. You can usually identify what section of the view is giving EDM problems.
这里有同样的问题,我所做的是在视图上添加 PrimaryKey,使用:
然后我必须对此进行 CAST:
该列必须是(不为空),这就是最后进行 CAST 的原因。
Same issue here, what I did was to add the PrimaryKey on the View, using :
And then I had to do a CAST of this :
The column must be (not null), that's why the CAST at the end.
就我而言,这是因为 OUTER JOIN。
这会导致列可为空并且无法由 EF 导入。
当我将其更改为 INNER JOIN 时,它起作用了。
另一种方法是使用 ISNULL (请参阅这篇文章的答案)
In my case it was because of an OUTER JOIN.
That causes the columns to be nullable and can't be imported by EF.
When I changed it to INNER JOIN it worked.
Another way is to use ISNULL (see the answer to this post)