Home > Enterprise >  I try make two relations one to many between 2 Entities but get Microsoft.Data.SqlClient.SqlExceptio
I try make two relations one to many between 2 Entities but get Microsoft.Data.SqlClient.SqlExceptio

Time:09-30

I read about some similar problems but I tried everything. I want to make 2 one-to-many relationships between classes Game and Team. One Team1 to many games, and one Team2 to many games.

I found some advice but it does not work. I tried with attributes [ForeignKey] and [InverseProperty] but the migration did not pass.

Then I used Fluent API - the migrattion works, but when I try to use GetAllGames method in Swagger, I get an error:

Microsoft.Data.SqlClient.SqlException: Invalid column name 'FirstTeamId'.
Invalid column name 'SecondTeamId'.

The database works in SQL Server Management Studio, it looks like a problem with Entity Framework.

I know was some similar problems but I tried it and I don't know what's next.

I use .NET 6 and Entity Framework Core 6.06

I will be grateful for any advice.

Github: enter image description here

enter image description here

CodePudding user response:

I tried your github project, and it works almost out of the box. I had to replace .\\SQLEXPRESS with (localdb)\\mssqllocaldb. In both places (TyperLeagueStorageContextFactory and appsettings.json). Update-Database works, query to /Games returns normal 200 OK response with empty array.

Without access to your complete environment, it's hard to say what did go wrong, easiest way out of this is probably to drop your database and start fresh.

(I think you are setting yourself up for lot of pain with some of the choices you made, but that's not the topic discussed here. Pain is how we learn and this looks like learning project.)

  • Related