Nuget package is referencing old database context from class library - c#

I currently have a class library project that contains an ADO.Net database context file and metadata classes for those context models. I'm using the database first approach and have added two new tables and modified one table. I'm able to build the project successfully and reference the new tables in a different project in the same solution but when I create a NuGet package for the Context class library and install the NuGet package in a new project, those new tables are not there and the table that was supposed to be updated is not.
Any ideas on what could be going wrong?

I was able to figure it out. Here are the steps I took to properly package it.
Update Assembly Info on project
Build Project on Release config
Create spec file 'nuget spec'
Pack into nupkg using -properties parameter 'nuget pack PROJ.csproj -properties Configuration=Release'
I was able to stumble upon the properties parameter here.

Related

Add Migrations to a class Library

I have a class library that contains my DBContext class and the entity classes.
I am using the class library as a nuget package in other projects - not in the same solution.
My plan is to be able to run migrations from the projects and let the migrations be stored in the context class Library under a folder names "Migrations".
So, after including the nuget package into the web project, i modify my Startup.css as follows:
services.AddDbContext<MyDbContext>(options => options
.UseSqlServer(Configuration.GetConnectionString("MyDbConnection"), m=>m.MigrationsAssembly("MyDbContextLibrary.Migrations")));
When I run this, I get the error message as follows
Could not load file or assembly 'MyDbContextLibrary.Migrations, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
I wish to get a guide on how to go about this.
What I am trying to achieve is such that all the migrations are contained in the DBContextLibrary so that any other project that includes it will have the migrations and can easily just run the "Update-Database" command without having to create separate migrations and another database. The plan is that all the projects can connect to the same database without having each project create their own database.
The project is running on EFCore 5.13 and ASP.Net Core 5
Please note, I already referred to the following SO questions and answer but yet to get a solution
Thank you

How to add project dependencies in a MSBuild solution programmatically with C#

Hello I ran into a XY problem using Visual Studio 2017 and dotnet 6.0.100
My original problem is: "How to add project dependencies in a MSBuild solution programmatically with C#"
This is needed to force a modular application to compile unreferenced projects in a .sln during the build process to also compile all other projects in the solution.
This .sln file is built programmatically and should also do this step automatically during creation of the .sln to reduce possible problems for the user working with this .sln.
The .csproj of the projects however should not be touched since they are part of a git submodule, that should not include these changes to the .csproj but only the changes to the .csproj made by the user.
Solution
There is a 10 year old solution described here and here using the DTE Interface.
Derived problem: I failed to include the correct references for the EnvDTE.DTE or EnvDTE80.DTE2 objects to be available in code.
I tried adding the Microsoft.VisualStudio.Setup.Configuration.Interop NuGet package and searched in the references for EnvDTE or Microsoft.VisualStudio.Shell as seen e.g. in the example here without luck.
Original problem
Manually the project dependencies can be added as described here
I tried adding the project dependencies via the dotnet sln command but didn't find a corresponding command to add the dependencies.
dotnet sln -h only points to add, list and remove. The add command didn't show an option for dependencies.
Since I already use dotnet commands to create the solution and add projects this would be a good solution for me if there is a way.
The other thing I tried was using Microsoft.Build.Construction:
SolutionFile solutionFile = SolutionFile.Parse(PROJECT_SOLUTION_FILE.FullName);
var projectInSolution = solutionFile.ProjectsInOrder;
which does enable me to read the projects in the sln but seemingly not to manipulate it since its all readonly.
Summary
I need a way to tell the solution that all projects have to be compiled on run/compilation of the designated one.
To accomplish this I either need a way to add project dependencies to the .sln file via
dotnet shell commands
something like Microsoft.Build.Construction
or a good source explaining to me how I can get the EnvDTE code running in my MS Visual Studio 2017
or there is some awesome way to do this that I didn't stumble across yet
I really appreciate any help to solving my problem.
Edit:
To clarify project dependencies are different from project references in .csproj files!
The project dependencies I refer to are this section in the .sln:
Project("{GUID}") = "PROJECTNAME", "CSPROJECT_LOCATION", "{GUID}"
ProjectSection(ProjectDependencies) = postProject
{GUID} = {GUID}
{GUID} = {GUID}
...
EndProjectSection
EndProject
I found the solution to my Y problem, adding the dependencies using EnvDTE as described in the answer.
The references for EnvDTE and Microsoft.VisualStudio.Setup.Configuration.Interop did not show up because I was using a SDK-Style project to test them, since we do use them by default currently.
Installing the Microsoft.VisualStudio.Setup.Configuration.Interop NuGet did not remedy this either!
According to this question SDK-Style projects do not support working with the DTE Interface yet.
That is the reason, why I was unable to find the references described in this example.
The example also mentions creating a .NET Framework Console App, which I did not see as a requirement at the time.
Summary:
To work with the DTE Interface in 2021 better use the old project format and avoid the SDK-Style project format!

How to reference Nuget binaries in Wix Bundle PayLoad?

I am publishing my Bootstrapper.dll on a local nuget server during its build, and this is working fine. But to use it in my Bundle projects (wixproj), I can't find any direct method to reference this in PayLoad.
As a workaround I have created a dummy C# project, adding the nuget reference, Copy Local=True and then referencing this dummy C# project's TargetDir\Bootstrapper.dll in Bundle PayLoad.
Question: Is there any way to avoid using dummy C# project and using nuget referenced binaries in Bundle's PayLoad directly?
Thanks
You can install the NuGet package in your Wix Bootstrapper project. Create a install.ps1 to update Payloads in your Bundle.wxs. At the end of install.ps1 save the project.
$project.Save($project.FullName)
Don't forget to undo the changes made in install.ps1 in uninstall.ps1 to keep everything nice an clean.
The above described procedure will save you from creating C# dummy project.

Create a Visual Studio Project Template that pulls NuGet references from online feed

I'm creating a Visual Studio Project Template and bundling it inside of a VS Extension. I need Projects created from the Template to reference ~20 NuGet packages.
Is it possible to have the references resolved from nuget.org rather than having to include all of the references inside the VSIX?
The NuGet documentation on Visual Studio Templates provides instructions on how to add packages inside the VSIX, but it requires the file be stored locally on disk and the .nupkg is bundles inside the vsix:
Add your nupkg files as custom extension content in your source.extension.vsixmanifest file. If you're using the 2.0 schema it should look like this:
<Asset Type="Moq.4.0.10827.nupkg" d:Source="File"
Path="Packages\Moq.4.0.10827.nupkg" d:VsixSubPath="Packages" />
Question already asked
I know a similar question was asked (Creating a Visual Studio Project Template that already includes a Nuget Package Reference?) and answered (not possible), but this was asked in 2011.
5 years later, is it still not possible?
Since there is still no Built-In functionality to Install/Upgrade packages from online Repo, here is a small workaround wich might help:
Prerequisites
First, install the NuGet.VisualStudio nuget package into your project.
You get that from here
When installed, the package will automatically set the Embed Interop Types property of the assembly reference to True. The reason it does so is to make your code resilient against version changes when users update to newer versions of NuGet.
For the same reason, you must NOT use any other types besides the above interfaces in your code. You must NOT reference any other NuGet assemblies either, including NuGet.Core.dll.
After setting up all that stuff, you can do the following in your RunFinished-Method:
var componentModel = (IComponentModel) Package.GetGlobalService(typeof(SComponentModel));
IVsPackageInstallerServices installerServices =
componentModel.GetService<IVsPackageInstallerServices>();
if (!installerServices.IsPackageInstalled(project, "Newtonsoft.Json")) {
var installer = componentModel.GetService<IVsPackageInstaller>();
installer.InstallPackage(
"All",
project,
"Newtonsoft.Json",
(System.Version) null,
false);
}
Note
That example shows based on Newtonsoft.Json how you can install a package.
For sure you can choose the projects targeting the installation. Also you can determine the Version to be installed.
It seems a bit uncomfortable, but unfortunately there is no other way around.
Usings
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
using NuGet.VisualStudio;
Let me know if that helps!
Yes, you can create a nuget package and add those other packages as its dependencies. Then when you download that package it will get all its dependencies and add to your project.

C# Multiple projects reference the same dll - Whats the easy way to update the dll?

I'm curious how others do it, or if there's an easier way...
I'm having multiple projects which are referencing the same DLL i.e. nhibernate.dll.
Those dlls are stored in version folders like NHibernate-3.2.0.
If a new version comes out and we are going to upgrade to it, we create a new version folder and need to update all referencing projects to the new dll.
Is there some easier way to do that? Like some central project (lets call it REF_PROJ) which references nhibernate and all other projects are only referencing REF_PROJ?
So the updating of the new version has only be done at one central place, the REF_PROJ.
Your solution is one way. The "better" way IMHO is to use nuget packages. Specifically the nhibernate nuget package. Then you can right click on the solution and update all the nhibernate packages when a new version comes out.
EDIT: To add to the other answer. If you wish to use a specific version of the nuget package that targets a particular version of the assembly in the package, use --version when you install them from the package manager console.
Just have a root folder, not version specific. Or if you really need version specific, then have 2 copies - one in the root folder and one in a version specific folder.
here is an example of what i mean.
nhibernate
--> nhibernate 3.2
----> nhibernate.dll
--> nhibernate.dll
Another solution is to using the GAC
register your assemblies into gac, and add reference to it from multiple projects without specifing assembly version . and update it easy with gac util.

Categories