Entity Framework 7 in Class Library - c#

Can I use a EF7 Code First in a Class Library to be referenced by a Console Application?
I'm preparing/creating a Database to be used in multiple projects in the future.
Now I need to play around with EF7 but not in ASP. Im ok with Console.
Now in a console application to management the first data.
Then in the Final Release of ASP.NET Core 1.
Thanks.

EF/NuGet
Assuming you are using Visual Studio, you would use the built in NuGet functionality to download and install the package. Here is a detailed tutorial.
The easiest thing to do from that link is to open the Package Manager Console window and type the following: Install-Package EntityFramework. This will install the latest version which happens to currently be 6.1.3.
EF Without NuGet
If you don't use Visual Studio or you cannot use NuGet for whatever reason, you can manually download the package from the EF NuGet Site and rename it from a .nupkg extension to a .zip extension and extract the DLL from the lib folder.
EF7
Now your question specifically mentions EF7 which is can be installed using NuGet with the following command: Install-Package EntityFramework.Commands -Pre in the Package Manager Console.
If you don't have NuGet, you can follow the steps above or download the source from GitHub and build the DLL yourself. Having said that, it is a RC so use at your own risk.

Related

How to achieve full NuGet reinstall for dozens of solutions?

We have dozens of solutions in a repository and we're retargeting every project to net472 from net462. Currently our best bet is to open each and every solution in Visual Studio and execute the following command in the Package Manager Console.
Update-Package -Reinstall -IgnoreDependencies
As far as I'm aware, the PM console cannot be used outside Visual Studio, so this method of course is not very efficient, so what I was thinking about is using the nuget.exe tool for this. However at first glance I could not find any equivalent operation or argument set.
The documentation at this moment says the following
For all packages, delete the package folder, then run nuget install.
For a single package, delete the package folder and use nuget install
to reinstall the same one.
So based on this I tried to delete the packages folder and run nuget install for a project, so I expected it to do a re-install. However, while it installed the package indeed (to packages), it does not touch the packages.config (for retargeting).
Is anyone aware of any kind of possible way to automate this process?
How to achieve full NuGet reinstall using nuget.exe CLI?
That command cannot get what you want.
As far as I know, nuget install should be with packages.config file and it will not update the nuget framework version of packages.config file automatically.
So whenever you change the target framework version of your project, using that command will not update the target framework version of the nuget package.
So only update-package -reinstall command under Package Manager Console will update the target framework version of packages.config file.
And also Package Manager Console cannot access multiple solutions so you have to open each solution to run that command.
Although it may be possible to achieve your expectations with PowerShell scripts, but it is too complex so that it is easier to open each solution and then run the command.
As a suggestion,
1) open each solution on VS to run update-package -reinstall command.
2) And migrating from packages.config to PackageReference may be a good choice. In this case, the nuget packages will automatically adapt to the corresponding project target framework version.
Before doing this,you can make a backup of your project.
3) If these all do not meet your requirements, you could suggest a feature on our User Voice Forum to report your desire for automation. After that, you can share the link here and anyone who is interested in it will vote it so that it will get more attention from Microsoft.

NuGet package fails to install in .NET Core, but works in .Net Framework. Why?

I am working on a Visual Studio solution with two projects which I will call Red.dll and Blue.dll for the purpose of this question. Note that Red.dll depends on Blue.dll. These are both compiled in .NET Standard 2.0 with the output type of "Class Library".
I added the dll of each project to a common NuGet package called "Red" using NuGet Package Explorer. It has the following structure:
lib
- netstandard2.0
- Red.dll
- Red.xml
- Blue.dll
- Blue.xml
Here is the problem: When I attempt to install this NuGet package to a .NET Core application, I receive the following error:
Error NU1101: Unable to find package Blue.dll. No packages exist with this id in source(s): Desktop, Microsoft Visual Studio Offline Packages, nuget.org
Blue.dll is not a NuGet package. I don't know why it thinks it is.
Blue.dll is located directly beside Red.dll.
There are no external dependencies other than Newtonsoft.Json which is irrelevant.
And to reiterate, Blue.dll is NOT a NuGet package dependency. I realize if you use the "Pack" feature in Visual Studio it will create a dependency, but I created this NuGet package manually to avoid that.
But most importantly: This all works perfectly in .NET Framework! The NuGet package successfully installed, both dlls are referenced, the code builds without any errors, and the XML documentation is accessible.
Why does this work in .NET Framework but not .Net Core?
I haven't found anything else online about this particular problem.

How do I install System.Drawing.Common in a project that uses '.NETFramework,Version=v4.5.2'?

I am trying to write some unit tests in C# in a '.NETFramework,Version=v4.5.2' application but all tests give the next error:
'System.IO.FileNotFoundException : Could not load file or assembly
'System.Drawing.Common, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file
specified.'
When I try to install System.Drawing.Common I get the next error from the NuGet package:
Could not install package 'System.Drawing.Common 4.5.1'. You are trying to install this package into a project that targets
'.NETFramework,Version=v4.5.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more
information, contact the package author.
I cannot change the application version or the framework (.NET Core 2.1) and any other trick I found online did not work (or generated more errors).
Help?
in NuGet put this line :
Install-Package System.Drawing.Common -Version 4.5.2
in .NET CLI put :
dotnet add package System.Drawing.Common --version 4.5.2
in Paket CLI put :
paket add System.Drawing.Common --version 4.5.2
Had the same problem. I have cloned solution https://github.com/barnhill/barcodelib . It has two projects: library project targets .Net Standard 2.0 and refers to System.Drawings.Common. Example project depends on library and has reference to System.Drawings.Common. Example project was not compiling due to same error.
My solution was just to remove reference to System.Drawings.Common in nuget packages and readd it (rclick on Example project > Manage nuget packages > Browse Installed, remove the System.Drawings.Common package, and then add it back), unload project and then reload it again
Helped for me.
I managed to solve it by restarting Visual Studio, changing the framework to 4.6.1 (which I could not do before) and adding the reference.
OP's solution migrated from the question to an answer.
you could try to use one of the libs described in here instead https://devblogs.microsoft.com/dotnet/net-core-image-processing/
you could try to use https://www.nuget.org/packages/CoreCompat.System.Drawing/ as well and maybe try to change your app framework to .net standard 2.0?
This is kind of bizarre but it worked dramatically so I'm going to mention it. I built a small vs 2017 ent console project that was supposed to read an oracle database. When I started to run it I got the System.Drawing.Common error mentioned above. It seemed stupid because I wasn't really doing anything having to do with Drawing at all. In Manage Nuget Packages I deleted the Oracle.ManagedDataAccess driver and added the Oracle.ManagedDataAccess.Core and the System.Drawing.Common error went away and I was able to read my oracle database. So I'm suggesting that with NuGet you may be picking up some things you really don't need and if you have any choice for your NuGet packages, try different ones. This also may be some foible with how my organization managed nuget for Visual Studio 2017 enterprise.

Adding a NuGet package to a project without Visual Studio

I'm trying to automate adding NuGet packages to project on a remote server that doesn't have Visual Studio (nor any build servers) installed. I do know how to use NuGet.exe from my C# code, but this executable only downloads packages and doesn't do any other required work (adding references, executing ps scripts etc). What do I do in order to fully install a package?
Note: I don't need to update a package for my own app, I need to add a package to an arbitrary .csproj file on the server. I'm building a Web-based .Net IDE, and need my users to be able to add packages to their projects.
I see two possibilities: one is using some kind of functionality not present in NuGet.exe, but ratherin some other library (maybe a VS addin), but I don't know where to look for it. The other is to simulate some kind of NuGet Powershell console and send commands to it, but again, I don't know how to do that.
You might want to consider SharpDevelop.
Installing NuGet Packages outside of Visual Studio an article about the functionality you are trying to achieve (written by Matt Ward, one of the project contributors) says:
Since NuGet uses PowerShell the simplest approach was to extend the
existing PowerShell cmdlets included with SharpDevelop. Now you can
write a few lines of PowerShell script to install a NuGet package into
a project that has never had a NuGet package before, have the project
itself updated and any package PowerShell scripts run. All this from
the command line without Visual Studio open.
Just to add an additional answer in-line with Alex's post about SharpDevelop, you have a couple different options.
Use Nuget.exe (Related Blog post)
NuGet Addin for MonoDevelop and Xamarin Studio (see GitHub)
ASP.NET Pages using WebMatrix (see this video)
All of this info is from the Nuget FAQ.

How to install Moq framework

I plan on using Moq to mock some interfaces in the unit test code I've created. I've already downloaded the latest version of Moq.
My question is how do I install it? Where should I place the Moq.dll?
I've tried searching on the internet, but all I can find are samples of how to use Moq, not how to install it.
The best way to add reference to Moq framework is installing it from Nuget. Also you still can download Moq.dll and add reference to this library (usually I create folder libs under the solution folder, where I put all third-party libraries, which is not available via Nuget).
BTW Another option to install package from Nuget - right click on project references and select Manage Nuget packages.... Then search online for Moq and install it. See why use Nuget over installing libraries directly on my machine
There's no need to install it. Just add a reference to the moq.dll in your project.
But of course you can use gacutil to register the library in your global assembly cache.
c:\path> gacutil /i Moq.dll
When using Visual Studio:
Right click on References [It's in the project Explorer]
Manage NuGet Packages
Search for Moq and add it to your solution.
This is an old question, but the convenient method I used is not listed here and this is the first result on google. I am using VS 2013 and if I search for Moq in Extensions and Updates there are no results so:
Go to Package Manager Console - (Tools -> Library Package Manager)
Change the default project to your test project
Then type: install-package moq
For .NET Core, using the dotnet CLI - dotnet add package Moq
You don't need to install it.
You could use NuGet of course (if you use newer versions of VS), but you can just copy it to your project folder (or preferably something like lib subdirectory of your project folder) and just add a reference to it.
EDIT:
You seem to have problem with wrong version. In your downloaded moq zip archive, there are multiple folders. You need to use one from folder Net35, not one from Net40. These numbers refer to the version of target .NET framework, not version of Moq itself.
When using Visual Studio
Right-click the test project in Solution Explorer
Manage NuGet packages...
Change Package source to All
Browse for Moq
Install
If you are on newer version of Visual Studio (2013+), you can use Package Manager Console.
Tools > Nuget Package Explorer > Package Manager Console
Execute:
Install-Package Moq -Version 4.5.16
Also see:
https://www.nuget.org/packages/Moq/

Categories