Reverse engineering using EF in core 1.0 - c#

I've got a Database in SQLServer 2016 and I'm trying to reverse engineer it in VS2015. Because I want the Models to be in their own class and Not directly in my MVC Project. I create a Core 1.0 Class Library project and I then followed the instructions here Reverse Engineer however although the package manager suggested all installed without issue, when I look in the project.json file, under dependencies it red lines "Micorsoft.EntityFrameworkCore.Tools": 1.0.0-preview2-final it states that this reference does not support framework .Net Standard,version = v1.6. It works if I do the same in an MVC/Web Api based project, but not a class library. Any ideas how I can get this to work, it's part of my separation concern and tiering.

within the Solution I created a standard class library project setting the target framework to 4.6.1. (not .NetCore). See screen shot attached. you can see the references and the project type. I used the reverse engineering guide from my question to this project. Once in here I simple copied the classes that I wanted to my .Net Core Class Library project. This all worked fine.

Related

EF Core 6 startup project doesn't reference EFCore.Design while trying to scaffold to a project that is NOT the startup project

I have a Web API solution with several projects inside it. Tests, Services, Domain, Data and the API. The API project is the startup project. All of my EFCore references are in the Data project.
I am trying to run Scaffold-DbContext on the Data project however because it's not the startup project I'm getting the following error:
Your startup project 'APIProj' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.
The API project shouldn't know anything about EF Core so I don't want to just slap that reference in there and call it a day. Trying to do things better than they were... I also don't want to make anyone else who uses this to have to juggle startup projects. This should be simple.
How do I correctly point the Scaffold-DbContext command at a specific project regardless of what is the startup project.
Using the EF tools requires an executable project,
So you'd either need to add that assembly to the executable project like you're suggesting yourself, or you can add a dummy executable project.
See for ex;
Why is a dummy project required? As mentioned earlier, the tools have to execute application code at design time. To do that, they need to use the .NET Core runtime. When the EF Core model is in a project that targets .NET Core or .NET Framework, the EF Core tools borrow the runtime from the project. They can't do that if the EF Core model is in a .NET Standard class library. The .NET Standard is not an actual .NET implementation; it's a specification of a set of APIs that .NET implementations must support. Therefore .NET Standard is not sufficient for the EF Core tools to execute application code. The dummy project you create to use as startup project provides a concrete target platform into which the tools can load the .NET Standard class library.
https://learn.microsoft.com/en-us/ef/core/cli/dotnet#other-target-frameworks
In our own project structure we have the design reference included in our executable project aswell. However, now that i'm thinking about this, our app gets rather large, and running the EF commands forces a rebuild of the executable project, so this means it will completely rebuild my large app which takes a long time.
Therefore I recommend using a dummy project, like MSDN also recommends.
Edit;
I think in theory you could also make your data project an executable project, and reference that instead:
// ConsoleApp1
new MyConsoleApp2Class().Print();
// ConsoleApp2
namespace ConsoleApp2;
public class MyConsoleApp2Class
{
public void Print()
{
Console.WriteLine("Hello world.");
}
}
This does output the exe in your /bin however:
This depends on the framework of your data project, .net standard projects cannot become executables.
You can then use a Design time db context factory to help instantiate the db context for the tools (with the right sql connection string and so on):
https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-a-design-time-factory
That way you don't need your data project to contain a whole application, as described in https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-application-services
The advantages of using the executable project of your app is that you can maintain appsettings for 1 project, the other approaches you'd have to save the connectiong string in 2 places, or have some logic in the design time factory to fetch the right connection string or something along those lines.
But then again how often does your db connection string change? In most cases almost never.

Antlr3 C# on .Net Core migration not finding partial class

I'm trying to migrate a project from .Net Framework 4.8 to .Net Core 3, said projects contains also a MathParser based on Antlr3.
I used the nuget package Antlr3.Runtime (3.5.1) and it seems to correctly create the files needed under MyProject\obj\x64\Debug\netcoreapp3.1 but then those files seems to be unreachable by the application.
To be more specific the created file MathParser.cs contains a piece of the partial class MathParser, the other part of which is in MathParser.g3.cs, but that is not considered.
In the starting project the files are created in the same way (and are identical) and the only exception is that they are in the subdir MyProject\obj\x64\Debug, without the additional folder "netcoreapp3.1".
I really don't get how the .Net Framework 4.8 can seemlessly load MathParser.cs after compiling (and creating it) but .Net Core can.
Any help? Thanks

Why does a dotnet Core App with a reference to a classic .Net project works and compiles [duplicate]

I'd really like to start using .NET Core and slowly migrate applications and libraries to it. However, I can't realistically upgrade my entire code base to use .NET Core and then go through the process of testing and deploying a plethora of applications in production.
As an example, if I create a new .NET Core application and try to reference one of my .NET Framework projects I get the following:
The following projects are not supported as references: -
Foobar.NetFramework has target frameworks that are incompatible with
targets in current project Foobar.NetCore.
Foobar.NetCore: .NETCoreApp,Version=v1.0
Foobar.NetFramework: .NETFramework,Version=v4.5
Is it possible to create a new .NET Core application and reference my existing .NET Framework libraries? If so, what's the process for doing that? I've spent hours going through Microsoft's documentation and searching their issues on GitHub, but I can't find anything official on how to achieve this or what their long-term vision is for this process.
Old question, but with the release of .NetStandard 2.0 and .netcore 2.0 and vs2017.3, the game has changed.
You can use the Full .NET Framework (TFM) with .NetCore 2.0, but how?
In Visual Studio 2017.3, you can reference the Full .NET Framework (any version) directly from within a .NetCore2 project.
You can build the .NetStandard2 class library and reference your TFM. Then reference your .NetStandard2 library from your .NetCore2 project.
For example, referencing json.net net45 from .NetStandard2.
Browse to the folder and select version net45 (not netstandard1.3)
See the dependency in the image below, no yellow warning as you see.
Even if a Nuget library is not ready to be ported to .Netstandard 2, you can use any API in the library that is compliant to net461.
Quoting for the .NET Core 2/Standard 2.0 announcement with links:
.NET Core 2.0 is able to freely reference libraries that have been built for .NET Framework up to version 4.6.1
However, some libraries may fail at run time if they try to use API methods that aren't available on .NET Core
Reference: .NET Core App target .NET framework 4.5.2 on Linux
A need to use third-party .NET libraries or NuGet packages not available for .NET Core
So only in cases where the libraries or NuGet packages use technologies that aren't available in .NET Standard/.NET Core, you need to use the .NET Framework.
Reference: Choosing between .NET Core and .NET Framework for server apps
You can now reference .NET Framework libraries from .NET Standard libraries using Visual Studio 2017 15.3. This feature helps you migrate .NET Framework code to .NET Standard or .NET Core over time (start with binaries and then move to source). It is also useful in the case that the source code is no longer accessible or is lost for a .NET Framework library, enabling it to be still be used in new scenarios.
Reference: Announcing .NET Core 2.0
Yes, we are currently attempting the same thing. The trick is to make sure that you are supporting the same .NET frameworks. Inside your project.json file, make sure the framework matches the framework of the project you wish to include. For example:
"frameworks": {
"net46": { --This line here <<<<
"dependencies": {
"DomainModel": {
"target": "project"
},
"Models": {
"target": "project"
}
}
}
},
FYI: You might need to change the framework of your .NET Core or your older projects to achieve this. .NET Core can be changed just by editing the project.json file as seen above. You can so the same in .NET projects by right clicking the project and opening properties. Change the framework level there.
Once you have matched the two project frameworks then you should be able to include them. Good Luck!
We delayed migrations as long as could as it seemed daunting as first. But we got an insistent client who wanted to migrate ASAP.
So we migrated their Fintech Web App developed on .NET Framework 4.8 Web Forms to .NET 6 Razor Page. Our team scoured though hundreds of online resources & spoke to Microsoft Tech Support before we started the project. Hope the high-level walkthrough of our journey help you plan your migrations.
Our .NET Framework Website consisted of 1 .NET Web Forms project and 12 Class Libraries.
Here is how we did it.
Refactored the .NET Framework 4.8 Web Forms code
We ensured that the Web Forms code behind did not have a single line of service or business logic code. When we did find some business logic code in the web forms code behind, we refactored it, by moving it to the class libraries.
Created new .NET Standard projects
We created a new .Standard 2.0 Class library project for every .NET Framework 4.8 Class Library. If the original project was called "FintechProjectName.StockMarketClient", we named the .NET standard project "FintechProjectName.StockMarketClient.Standard".
Copied all files from .NET framework to .NET standard
We copied all the class files from .NET framework to .NET standard projects. We then removed all the .NET framework class libraries from the solution and added references to the new class libraries. All projects compiled on the 1st try itself and all our test cases too passed with minor changes.
Create new .NET 6 Web App Project
We created a new .NET 6 Web App Project. We had to entirely redo the front-end as there is no direct path for migrating Web Forms to Razor Pages. This was the only project which took us about 1 month to migrate.
Reference .NET standard class libraries in the new .NET 6 website
We copied all the .NET Standard libraries to this new solution containing the Razor Pages web site. Added the references and got it to work.
Move from .NET Standard to .NET 6 class libraries
Once the new website was up and running, with all test cases passed, we did the last step in the process which was the simplest. Created .NET 6 class library projects for each of the .NET standard libraries and named the projects appropriately. Copied all class files from .NET standard projects to their corresponding .NET 6 projects. Then we removed the .NET Standard libraries and added references to the new class libraries.
Overall project timelines were about a month and a half, most of it spend on Razor Pages implementation using the same html design.
Note:
If you are using any 3rd party library which does not have a .NET standard or .NET 5 version, then you are out of luck. You will need to find a replacement nuget package and recode your application to use this new library.
In my case with .net6 referencing framework 4.8 library ( both winforms), the trick seems to be to add the reference to the framework dll as a shared reference.

.Net Standard (Not Core) Per Environment Configuration

I am currently converting a .Net Framework class library to a .Net Standard class library. In Framework we used app.config and had transform files to hold the environment specific data such as connection strings for the environment (Prod, Test, Dev).
I have scoured SO and Google for this answer but keep coming up empty. What is the best way to store and retrieve this data in a .Net Standard class library? At this point I don't care if I have a bit of code that populates properties based on my Build Settings or if I have a file sitting next to the DLL that houses this data. I'm just needing a way to connect to ServerA in dev and ServerB in prod.
I'll have either a .Net Core or .Net Framework project that references this DLL and asks this DLL for data but I want this library to house it's own DB connection, not passing it from the hundreds of apps calling it.
Turns out System.Configuration.ConfigurationManager was added back in .NETStandard 2.0.
Just pull it in from nuget and compile the .NETStandard 2.0 class library project.
Then, the library will work across projects using standard config files:
Net Core 2.0 projects use app.config
Web projects work from web.config
Console and Windows apps work with app.config
(This is of course assuming you're going to .NET Standard 2.0)
Then, here's a post I found that discusses how to use ConfigurationManager in case you need some examples.

Class library references on Visual Studio 2015

I've added a new Class Library (Package) project to my solution. It's my first experience with a .NET Core (or whatever I'm using, still confused)
My class library contains two references: .NET Framework 4.5.1 and .NET Platform 5.4
I'm trying to import some code from a sample project that uses IPrincipal. For some reason it's saying that it doesn't exist on namespace "System.Security" altohugh I can get it trough intellisense.
What's wrong with my project settings?
The new feature of .NET Core and Class Library (Package) is that it targets multiple platform and will compile into multiple assemblies which get automatically packaged into a nuget package.
When your class library targets multiple targets, it will compile to all of them. So if a certain library is only available on full .NET framework but not on .NET Core or other target framework, then you may receive intellisense if your editor is set to .NET 4.5. More information can be found in my other recent answer.
You can switch back and forth with the pull down menu on top left of the coding window, show in the screenshot below.
If you do not want to target a certain framework, you have to remove it's moniker from the project.json file or use preprocessor directives to write platform specific code or libraries/replacements.
.NET Core is heavily modularized and most of only the core modules are referenced in the default project and if you need additional one you need to reference them within the dotnet5.x section.
Basically you have multiple places with "dependencies" in your project.json, a global one where you can add dependencies which are available on all targeted frameworks and one within each "frameworks" section for each of the targets only.
Though the other answer covers some basic concepts, it would require some attention on which classes are available and which are not.
Microsoft temporarily host a web site at http://packagesearch.azurewebsites.net to assist.
If you can find a suitable package for RC1 from there, then you can add it to your project.json file. If not, you will have to conditional compile it to a desktop profile or use other alternatives.

Categories