iT邦幫忙

1

【已解決】.Net Core升3.0 一問

  • 分享至 

  • xImage

目前有個專案從.net Core 2.2 要升3.0 還會慢慢爬到6
不過在3.0這塊卡到一個地方

使用 VS2019

依照官方文件

https://docs.microsoft.com/zh-tw/aspnet/core/migration/22-to-30?view=aspnetcore-6.0&tabs=visual-studio

專案檔要增加

<FrameworkReference Include="Microsoft.AspNetCore.App" />

Startup.cs

//已新增
using Microsoft.Extesions.Hosting;

//於Startup必定會用到的IHostingEnvironment 我換成 IWebHostEnvironment 

但是會顯示The type or namespace 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?)

不曉得還有遺漏什麼地方?

到處找的解答,對我也沒用處
https://social.msdn.microsoft.com/Forums/en-US/9890ce4a-23ba-4e21-8b36-7b266ea96a01/the-type-or-namespace-iwebhostenvironment-could-not-be-found-i-already-added-the-required?forum=aspdotnetcore
https://stackoverflow.com/questions/62927300/cant-find-iwebhostenvironment-in-microsoft-aspnetcore-hosting-abstractions-asse

真是一個頭兩個大,我翻遍升級的部落格,文章,筆記
黑暗大...都看過了,大家都正常套用到,我不知道出了什麼問題@@
不知道有沒有大大有經驗呢?

感謝~~

2022/07/27補充

找到個超多人回復有用的解答,還是無解
https://github.com/dotnet/Scaffolding/issues/1186

已解決

看更多先前的討論...收起先前的討論...
deh iT邦研究生 1 級 ‧ 2022-07-26 22:04:50 檢舉
雖然不懂為啥是3.0不是3.1
但要一直升上去有啥原因嗎......感覺很多坑
會去3.1的,不過要一步一步來,雷太多會踩不完XD
雖然3.0到3.1我看了一下是沒什麼太大變化~但還是按步驟來。

原因阿~ 資安問題呀...甲方問題....還有不升6的話,3.1也快要不支援嚕
rofellos iT邦新手 2 級 ‧ 2022-07-27 10:20:41 檢舉
VS2019不會幫你自動升3.0?
@rofellos
不會喔 有很多要改0.0
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
科科
iT邦研究生 1 級 ‧ 2022-07-27 09:30:53
最佳解答

我個人是用3.1 ,這個你有使用嗎?
Startup 中我用的是

using Microsoft.AspNetCore.Hosting;

有的話你有重新編譯一次看看嗎?

或是你建立一個你要的版本的新專案,比對一下差異看看。

看更多先前的回應...收起先前的回應...

實驗中...

Build都OK了,但跑 IIS Express,404 XD

科科 iT邦研究生 1 級 ‧ 2022-07-27 15:53:50 檢舉

那就是往下一步debug了 XD

可能是預設啟動的路由沒設定好
或是沒有對應的路徑
3.1 是用

app.UseEndpoints(endpoints =>
       {
             endpoints.MapRazorPages();

             endpoints.MapControllerRoute(
                 name: "default",
                 pattern: "{controller=Home}/{action=Index}/{id?}");
            });

嗯嗯,這個有改到,總算執行OK了,資料庫也沒問題。
接下來就要跑流程測試了

又撞到一個詭異的問題

Entity的Include

//這邊會產出莫名其妙的SQL
context.Bs
.Include(i => i.IndsCodeNavigation)

//詭異SQL
SELECT *
FROM [Bs] AS [b]
LEFT JOIN [Inds] AS [i] ON [b].[IndsCodeNavigationIndsCode] = [i].[IndsCode]

//正常的SQL應該要是
SELECT *
FROM [Bs] AS [b]
LEFT JOIN [Inds] AS [i] ON [b].[IndsCode] = [i].[IndsCode]

//不知道3.1修正了什麼變這麼詭異XD


//class
class Bs
{     
public virtual City City { get; set; }

public virtual Inds IndsCodeNavigation { get; set;}
}

已解,在Context裡面指定ForiegnKey即可。2.1時不需要的XD可能更嚴謹了

科科 iT邦研究生 1 級 ‧ 2022-07-27 18:19:44 檢舉

恭喜你又往下邁進一步~

我要發表回答

立即登入回答