无法将视图添加到 EF 数据模型

发布于 2024-11-08 06:18:08 字数 7062 浏览 5 评论 0原文

我有一个正在尝试添加到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

江湖正好 2024-11-15 06:18:09

为了能够将视图添加到模型中,实体框架需要数据库视图中至少有一列不可为空

To be able to add a view into a model, Entity Framework needs at least one column in database view to be not nullable.

居里长安 2024-11-15 06:18:09

检查您的视图是否确实是 *.edmx 文件中的视图。

正确:

<EntitySet Name="SomeView" EntityType="Model.SomeView" store:Type="Views" Schema="dbo" />

错误:

<EntitySet Name="SomeView" EntityType="Model.SomeView" store:Type="Tables" Schema="dbo" />

Check if your View is for sure View in *.edmx file.

Correct:

<EntitySet Name="SomeView" EntityType="Model.SomeView" store:Type="Views" Schema="dbo" />

Wrong:

<EntitySet Name="SomeView" EntityType="Model.SomeView" store:Type="Tables" Schema="dbo" />
南薇 2024-11-15 06:18:09

您可以向视图添加行计数,并使其看起来像不为空的关键“Id”字段。一个例子:-

SELECT DISTINCT
     -- dumb key for EF in C#
     IsNull(cast(ROW_NUMBER() OVER(PARTITION BY [Notes] ORDER BY [Notes] ASC) as int), 0) as [Id]

     -- required fields
    ,[Notes]        as [Notes]
    ,Count([Notes]) as [NoteCount]
FROM
    [dbo].[Communication]
GROUP BY
    [Notes]

You can add a rowcount to the view and make it look like a key "Id" field that is not null. An example:-

SELECT DISTINCT
     -- dumb key for EF in C#
     IsNull(cast(ROW_NUMBER() OVER(PARTITION BY [Notes] ORDER BY [Notes] ASC) as int), 0) as [Id]

     -- required fields
    ,[Notes]        as [Notes]
    ,Count([Notes]) as [NoteCount]
FROM
    [dbo].[Communication]
GROUP BY
    [Notes]
给妤﹃绝世温柔 2024-11-15 06:18:09

您在视图中添加了主键以外的字段,实体是坏男孩

You add the field than primary key in your view, entity is bad boy with it

椵侞 2024-11-15 06:18:09

我试图加载具有单个可为空列的视图。我的解决方案是将可为空的列包装在 isnull 中并提供默认值零:

isnull([column], 0)

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:

isnull([column], 0)
葬シ愛 2024-11-15 06:18:09

这可能是有关该主题的答案的组合,但我读过的所有内容似乎都没有提到这个特定的修复。
我的视图连接了三个表,并且我收到了如上所述的错误。我从两个表中选择了 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.

温柔嚣张 2024-11-15 06:18:09

在视图中添加 id 列:

isnull(ROW_NUMBER() OVER (PARTITION BY e.CCT ORDER BY e.CCT), 0) AS Id

之后,您可以在实体模型中添加视图。

Add id column in your view:

isnull(ROW_NUMBER() OVER (PARTITION BY e.CCT ORDER BY e.CCT), 0) AS Id

After that, you can add your view in the Entity Model.

不可一世的女人 2024-11-15 06:18:09

添加一个别名为“id”的虚拟字段,该字段不为空

...fields, 1 as id

Add a dummy field with an alias "id", which is not null

...fields, 1 as id
箜明 2024-11-15 06:18:09

只用了这个,其他都没用。我已经经历过几次了,主键 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.

兔小萌 2024-11-15 06:18:09
  1. 创建一个带有主键字段的支持表并仅插入一条记录。
  2. 创建视图并加入该表的创建列(键)。
  3. 现在您可以将视图添加到实体模型中。
  1. Create a supporting table with a primary key field and insert only one record.
  2. create view with with joining to the created column(key) of that table.
  3. Now you are able to add the view to the Entity Model.
红尘作伴 2024-11-15 06:18:08

如果您的视图不包含主键列,则它不会添加到 edmx 文件。

If your view does not contain the primary key column,then it wont add to edmx file.

故人爱我别走 2024-11-15 06:18:08

当我尝试添加不从另一个表中选择主键的视图时,我也遇到了同样的行为。 (就像 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.

终止放荡 2024-11-15 06:18:08

这里有同样的问题,我所做的是在视图上添加 PrimaryKey,使用:

..... (SELECT      TOP (100) PERCENT ROW_NUMBER() OVER (ORDER BY R.Road DESC) AS RoadNumber......

然后我必须对此进行 CAST:

ISNULL (CAST(RoadNumber AS INT),0)AS RoadNumber

该列必须是(不为空),这就是最后进行 CAST 的原因。

Same issue here, what I did was to add the PrimaryKey on the View, using :

..... (SELECT      TOP (100) PERCENT ROW_NUMBER() OVER (ORDER BY R.Road DESC) AS RoadNumber......

And then I had to do a CAST of this :

ISNULL (CAST(RoadNumber AS INT),0)AS RoadNumber

The column must be (not null), that's why the CAST at the end.

夜深人未静 2024-11-15 06:18:08

就我而言,这是因为 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文