C# EF Migrations don't create table - c#

It seems like I kinda broke my project. When I use e.g. add-migration initial to add a new migration, it generates a migration file in the project folder which looks correct, but I dont see it in my solution (in VS 22). When I try to update the database, it just generates the __EFMigrationsHistory -table, but everything else is missing and the command ends with No migrations were applied. The database is already up to date.
It feels like i broke the whole migration thing. Does someone has an idea on this?
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
</ItemGroup>

Related

C# Project Reference Warning Type confilict

I have a main asp.net core project which is called "API" and inside of it I have another project called "API.Tests" for xunit tests. So when I'm referencing main project (<ProjectReference Include="..\API.csproj" />), to get access to main's project classes, I'm also referencing everything that's inside of a "API.Tests" project since it's inside of a main project. That's why this warning appears when I'm trying to inherit one class from another in test project:
This is API.csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateProgramFile>false</GenerateProgramFile>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
This is API.Tests.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Nullable>enable</Nullable>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<IsPackable>false</IsPackable>
<TargetFramework>net6.0</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2"></PackageReference>
<ProjectReference Include="..\API.csproj" />
</ItemGroup>
</Project>
Please help, I don't know how to fix this.
The only thing you can and should do is put the production code in a sub-project as well and reference that sub-project from API.Tests instead of the main project.
i.e.
API
\API.Application
\API.Tests
with API.Tests referencing API.Application.

Unable to create an object of type 'ApplicationDbContext' in asp .net core 5

I made a project with 5 Class libraries in asp .net core 5.
'ApplicationDbContext' class is in the 'Data' Class library and ny models are in the 'Model' class library. when I run 'add-migration' in Package Manager Console, it works properly but when I run 'dotnet ef migrations add' in console, it shows me this error: 'Unable to create an object of type 'ApplicationDbContext''.
Here are the Nuget packages needed:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
1.Open the package management console: Tools->Nuget Package Manager->Package Manager Console
2.cd {path where your csproj file resides}
3.Now type dotnet ef migrations add InitialMigration

Coverage always at 0% on sonarqube even i added test unit

I created a test unit for a class and it passed well on my local, on sonarqube it is shown as 0% for Coverage, I found post advice to add coverlet.msbuild I added but still no news:
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
Any idea?
Try adding the following to your .csproj
<PropertyGroup>
<DebugType>Full</DebugType>
</PropertyGroup>
<PackageReference Include="Microsoft.CodeCoverage" Version="16.9.4" />
Can you try coverlet.collector instead of coverlet.msbuild? I have personally used coverlet collector several times successfully.
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
According to this GitHub issue.
Collectors are pretty new(last arrived) and are the best way to do coverage because are strictly integrated with vstest platform(dotnet test) and due to that is the default choice for every .NET core xunit project by Microsoft.
So if you create xunit project Microsoft xunit template inject by design our coverage engine as first class coverage tool running with dotnet test --collect:"XPlat Code Coverage"
It may be possible that you are running into this known issue.

EF Core "no migrations found" despite Migrations folder containing migration files

We have a .NET Core project set up with a Migrations folder containing several migrations that we were able to successfully run in our Development database in the past by executing dotnet ef database update. However, we are now trying to apply those changes to our Staging database and it isn't working. We've set our ASPNETCORE_ENVIRONMENT env variable to Staging and when we attempt to run the migrations, it seems like it is targeting Staging properly. A dbo.__EFMigrationsHistory table is created in our Staging database, but there are no entries in it. Also, when we run dotnet ef migrations list, we get a message that says "No migrations were found." Even if we set the ASPNETCORE_ENVIRONMENT variable back to Development, we get the same result.
We've run all the above dotnet ef commands with the verbose flag and haven't gotten any errors. The only output that seems like it could be suspicious is No referenced design-time services were found and No design-time services were found, but we weren't able to determine what we need to do to fix these issues.
When we run dotnet ef migrations add <migration name>, it successfully generates a migration file in the Migrations folder, and when we run dotnet ef migrations list that new migration shows up, so it seems like our older migrations are in the correct place. They just aren't being detected by dotnet ef for some reason.
We have the following packages in our .csproj:
<PackageReference Include="FlexLabs.EntityFrameworkCore.Upsert" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.113">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0" />
Any thoughts on why we can't get our existing migrations to run?
It turns out this error was because of the lack of Designer.cs files. We deleted them due to the response in this post, but EF was not able to find our migrations without them. If you plan on running your migrations in any other environments, don't delete your Designer files!

Is there a way to create a .Net Core class library that reference ASP.NEt core without beeing a netcoreapp

We are making a app template that make use of a shared API library with API controllers. (Avaiable from Nuget)
Since API controllers take use of Microsoft.AspNetCore we are forced to make the library target netcoreapp30.
That again forces us to create startup.cs and program.cs in that library to make it compile.
Is there a way to make a class library refernce needed aspnetcore stuff without beeing required to create the startup.cs and program.cs? (compiler complains about missing start point without them)
Startup.cs and program.cs we have in the app using the nuget.
Our project file for the library
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
</ItemGroup>
</Project>

Categories