How to enable code-first migrations - asp.net

There are many examples out there including this.
The problem is that many of the commands are changed, different, or not included with Entity Frameworks.
Can someone tell me how to enable code-first migrations with my project?
I am using visual studio 2015 and the latest version of MVC. My project works on the first build, but then after I kill the app and add another model, I get an invalid column name or suggestion to enable code first migrations.

Related

Azure Pipeline : ASP.NET and .NET Core applications under one build pipeline

Background:
I have several ASP.NET projects under one solution file. I have a build pipeline for ASP.NET projects and building it with .sln.
Now, we have added a new project to the same .sln which is in .NET CORE.
Problem statement:
In my build pipeline, when I try to build the whole solution, it throws an exception to the newly added project. (.NET CORE)
"The type or namespace name 'Entity' does not exist in the namespace
'System.Data' (are you missing an assembly reference?)"
I believe this is due to framework versions.
Question:
Is it possible to have both projects under one build pipeline, if yes, how can I achieve it? if not, what would be the ideal solution?
Additional information: From the visual studio, everything is building and working fine, but I build the project pipeline it's throwing this error.
Initially build the code from visual studio to avoid errors while building in the azure pipeline.
Used different projects in azure pipeline
Create a repository using Visual Studio or Azure Devops.
Fill the necessary fields while creating a repository
Then it enters into a Git Credential Manager screen and have to choose the account from which you want to create and proceed further.
Create branches if required.
Using Visual Studio, we can sync and push the changes made from the specific branch
Create a pipeline with azure subscription and build the code.
Refer this link for more info.
You could add a reference to System.Data.Entity to test if the exception is thrown again.
Or you could refer to this document for more details about Organize your project to support both .NET Framework and .NET
================================================================
updated on 11/30
This error message indicates the build process can't find the assemblies coming from the restored nuget packages. So you should check the log of the restore task in build-pipeline to make sure the packages are restored successfully.
Make sure you have use nuget tool install and nuget restore tasks before your build task like this, if your build include private feed, please add the nuget authenticate task to restore the project correctly. (Avoid using dotnet related tasks cause your project targets .net framework, and dotnet task could not recognize .sln projects)
I was able to resolve this by updating the NuGet package version for the pipeline. Same as the application version.

what is the best way to find out if a nuget package is compatible with .net core without nuget.org?

I know nuget.org does not have this functionality yet, but I have been searching for release notes on the nuget package developer websites, and this is taking longer than expected, since I have a lot of nuget packages installed on my .net framework project.
Is there a better way to do this? maybe someone has already done it and posted a list somewhere?
thanks in advance
If you change the 'n' in the nuget URL to an 'f', so it becomes fuget, you'll get a list of which frameworks the package targets. If you see it targets a netstandard version then it will work with .NET Core.
If your project is using an "old" style csproj with packages.config, the first step is to migrate to using PackageReference instead. Here's some docs. As the docs say, there are some differences between how packages.config and PackageReference works. If you're affected, you're blocked until you can make your project work with PackageReference.
If your project is using an "old" style csproj with PackageReference (for example you did the migration above), then migrate to SDK-based csproj so you can build with the dotnet CLI. Here's a blog post with details how to do it.. Note you you can keep using the Windows .NET Framework with SDK csproj. Although SDK-based csproj came out at the same time as .NET Core, it's not necessary to use .NET Core with the new project style. If your project is a class library or console app, you're definitely fine, otherwise you need to research to find out if the project type is compatible with SDK projects or not.
Once you have your .NET Framework project working with SDK projects, either change the TargetFramework to netcoreapp or netstandard, or you can multi-target your project by changing TargetFramework to TargetFrameworks, and use a semi-colon separated list of TFMs you want to target. For example <TargetFrameworks>net461;netcoreapp2.1</TargetFrameworks>. Then simply run dotnet restore and if any of the packages you use is not compatible with .NET Core, restore will fail, and you simply revert to target only .NET Framework.
In summary, once your project uses SDK-based csproj, it takes 10 seconds to test if your dependencies are compatible with .NET Standard/.NET Core. If your project is not yet using SDK-based csproj, you undo your change to the TargetFramework(s) line in your csproj and continue with your life until the next time you test again. If you're not already on SDK-based csproj and there's nothing blocking you from doing so, then doing the upgrade is low risk and bring some benefits, such as fewer merge conflicts on the file, much easier to create nupkgs for any packages you maintain, and being able to test against .NET Core compatibility in seconds.
Alternative: If you're unable or unwilling to migrate to SDK-based projects and you want to check if your dependencies are compatible, then use dotnet new classlib to create a new .NET Core project, add package references to the same packages that your existing project uses, then try to restore. If you have a big solution with lots of projects and/or references, just write a small program to read your packages.config/csproj files as XML, find unique list of packages that you use, then write a new SDK-based csproj targeting .NET Core with all the packages you just found as package references.

Project not compatible with netcoreapp2.0

I'm trying to add a full framework class library as a project reference to asp.net core 2.0 MVC project and getting the below error.
Project XYZ is not compatible with netcoreapp2.0 (.NETCoreApp,Version=v2.0).
Project XYZ supports: net462 (.NETFramework,Version=v4.6.2)
I have updated to the most recent version of Visual studio i.e, 15.3.5.
Is it even possible to reference 4.6.2 libraries in core 2.0 projects?
The first thing that you can try is to compile the library you want to consume as netstandard2.0.
Theoretically (according to the .net standard documentation), this will make it compatible with projects using net461 and later as well as netcoreapp2.0 and later.
In practice, sometimes you will end up with a problem with one of your dependencies that don't provide the same library version across different compilation targets.
In such cases you may simply need to add the .net core 2.0 as a target framework for the XYZ library.
The xml tag listing the targets is <TargetFrameworks> in the XYZ.csproj file and is not handled by the Gui of the project's properties.
So I would give a try at editing the XYZ.csproj by hand and add or replace what's listed as <TargetFrameworks> with netcoreapp2.0.
If you are adding it as additional target you need to separate them with ';' as in
<TargetFrameworks>net462;netstandard2.0;netcoreapp2.0</TargetFrameworks>
More details about this in this Microsoft doc.
Please keep in mind that this will trigger multiple compilations and will slow your build consequently...
It should be. Microsoft announced a ".NET Framework Compatibility Mode" with the release of .NET Standard 2.0. However, they didn't go into great detail about how it works exactly, or what to troubleshoot if it doesn't. Additionally, they only specific talk about it in relationship to Nuget packages, so it's possible there's some role Nuget is playing in the process, as well. Unfortunately, I've been unable to find any additional information about this feature outside of the announcement post.
That said, Microsoft's explicit recommendation is to not rely on the fact that your .NET Framework library may just happen to work in .NET Core; instead, you should be actively porting .NET Framework libraries you control to .NET Standard. I'd say you're likely going to spend more time trying to figure out why it doesn't "just work" than you would porting your code, so that it will definitely work, and be future-proof to boot.
The following solution worked for me.
Deleted bin and obj folders from all the projects in the solution, rebuild and if it still doesn't work try changing browser from debug options. for eg. If you already have chrome as default browser in Visual studio, switch to Edge or Firefox.

Cannot Create migrations for EF Core Sql lite in UWP app

I am using VS 2015 below is a copy of my project.json file. The project is a UWP app targeting 10.0 build 14393.
I've taken over this app and it has previous migrations.
When I try to run the Add-Migration command from the nuget Package manager I am getting "The EntityFramework package is not installed on project 'XXX'."
This project is the same project that has the project.json file below. My entities are not in a separate class library. I can't seem to figure out how these migration scripts got created and how to create new ones going forward.
Thank you all for your help. For those that may still have this issue I discovered it was actually due to the fact that I had a second project that had a reference to EF6 in it. Because of this the Package Manager was conflicting on the shared EF6 and EF Core commands. This worked fine once I prefixed my commands.
Invokes the EF Core command
PS> EntityFrameworkCore\Add-Migration
Invokes the EF 6 command
PS> EntityFramework\Add-Migration
Referenced in this article but was something I overlooked:
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell
I used the same environment to test the "Add-Migration" command. There's no problem. It worked well. See the following screenshot:
Please check the default project in your Package Manager Console to see if it's your current project.
You can see the following screentshot, my project's name is "SqliteCore", so the default project in Package Manager Console is the same.

why other project reference is not showing in namespace

I have three projects in one solution (ASP.NET)
ECL.BIZ.LOGIN
ECL.MODEL.LOGIN
ECL.UI
When I added a reference to other projects in ECL.UI project, namespace is not available in using keyword. My all projects are set to .NET Framework 4 (not ClientProfile).
Why is this happening?
Note: It was working earlier smoothly, it is happening since I deleted ECL.UI project and recreate it again. After then, I still can not find namespace of other projects.
I am using Visual Studio 2012
The answer of my question is:
We have to switch between target frameworks. As I am using Visual Studio 2012, I switched my target framework to .NET 4.5 for all projects and add the reference for required projects and switch back to .NET 4 again for all projects at same time. Then it works absolutely fine as I expected.
Note: I don't know why it is not working while all other projects are set to same framework even after recreating one of the project. However the above trick worked for me. If anyone having this weird problem after deleting one of their project and recreating it again, you can try the above on your own. Thanks to all

Resources