Answered by:
Blazor Server + WebAPI/EF Core + NSwag = Cannot implicitly convert type 'System.Collections.Generic.ICollection<BtServer.Transaction>' to 'System.Collections.Generic.ICollection<BtClassLibrary.TransactionModel>

Question
-
User379720387 posted
Cannot implicitly convert type 'System.Collections.Generic.ICollection<BtServer.Transaction>' to 'System.Collections.Generic.ICollection<BtClassLibrary.TransactionModel>'. An explicit conversion exists (are you missing a cast?)BtApiCore (NET5.0) has the EF Core Entity Transaction in the Model folderBlazor Server has NSwag that connects to BtApiCoreThere is also a BtClassLibrary which has the TransactionModelWhat is the problem here?
The squiggle is highlighted
@code { private ICollection<TransactionModel> transactions; //private DisplayListClientsModel listClient = new DisplayListClientsModel(); protected override async Task OnInitializedAsync() { swaggerClient client = new("https://localhost:44317", http); transactions = await client.TransactionAsync(); } }
Thursday, December 24, 2020 3:24 AM
Answers
-
User475983607 posted
This is the same problem as your other thread. You defined a member type as...
private ICollection<TransactionModel> transactions;
Then you try to stick a different type (ICollection<BtServer.Transaction>) into "transaction".
transactions = await client.TransactionAsync();
You can't do that! As explained in your other thread C# is a strongly typed language. The types must match.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 24, 2020 1:54 PM -
User379720387 posted
Made a change like this, and now it compiles:
private ICollection<BtServer.Transaction> transactions; protected override async Task OnInitializedAsync() { swaggerClient client = new("https://localhost:44317", http); transactions = await client.TransactionAsync(); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 24, 2020 5:00 PM
All replies
-
User475983607 posted
This is the same problem as your other thread. You defined a member type as...
private ICollection<TransactionModel> transactions;
Then you try to stick a different type (ICollection<BtServer.Transaction>) into "transaction".
transactions = await client.TransactionAsync();
You can't do that! As explained in your other thread C# is a strongly typed language. The types must match.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 24, 2020 1:54 PM -
User379720387 posted
The code you helped me with that works:
private List<TransactionModel> transactions; protected override async Task OnInitializedAsync() { transactions = await _db.GetTransactions(); }
GetTransactions() returns public Task<List<TransactionModel>> GetTransactions()
Then the API version
private ICollection<TransactionModel> transactions; protected override async Task OnInitializedAsync() { swaggerClient client = new("https://localhost:44317", http); transactions = await client. }
ICollection vs List, same TransactionModel
Then the highlighted: async client.
Intellisense gives me BaseUrl, Equals, GetHashCode, GetType, ReadResponseAsString, ToString, TransactionAsync, WeatherForecastAsync
Sorry, not seeing it.
Thursday, December 24, 2020 3:07 PM -
User475983607 posted
Essentially you are trying to force a round peg in a square hole. The problem you created is like this...
int num = "Hello World";
A string is not an int type and produces a cast error.
Thursday, December 24, 2020 3:51 PM -
User379720387 posted
Made a change like this, and now it compiles:
private ICollection<BtServer.Transaction> transactions; protected override async Task OnInitializedAsync() { swaggerClient client = new("https://localhost:44317", http); transactions = await client.TransactionAsync(); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 24, 2020 5:00 PM