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
Related
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.
Curent status
I have got the following setup (simplified example):
MySolution
┠ MyPclLibrary (PCL targeting .NET Standard 1.3)
┃ ┗ Referencing Microsoft.EntityFrameworkCore.Sqlite (NuGet package)
┕ MyAndroidApp (Xamarin Android)
┗ Referencing MyPclLibrary
In other words MyAndroidApp references MyPclLibrary which in turn references Microsoft.EntityFrameworkCore.Sqlite installed as a NuGet package. I work with Visual Studio 2015 Update 3 on a Window 7 SP1 machine.
The problem
Now the problem is that when I try to build the solution I get the following error message:
Exception while loading assemblies: System.IO.FileNotFoundException:
Could not load assembly 'Microsoft.EntityFrameworkCore.Sqlite,
Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
Perhaps it doesn't exist in the Mono for Android profile?
I have already checked the MyAndroidApp\bin\Debug directory and I can see that the Sqlite DLL has NOT been copied to the output directory.
Of course, MyAndroidApp does NOT make direct calls to Microsoft.EntityFrameworkCore.Sqlite. Instead, MyPclLibrary is responsible for communication with Sqlite.
My best guess it that this problem is somehow related to the new dependency management based on project.json (MyPclLibrary uses project.json while MyAndroidApp uses packages.config).
What have I already tried?
Direct reference (works but is not acceptable): An easy fix would be to add reference to Microsoft.EntityFrameworkCore.Sqlite directly from MyAndroidApp - so make the library a first class reference. However this completely defies the solution architecture and therefore is not acceptable.
Dummy class: I thought the problem might be that there is no obvious usage of the Sqlite (as it is basically a set of extension methods). Therefore I created a dummy class with a single method which enforces a call to the Sqlite so the Sqlite may not be optimized out - to no avail.
No Linker in Adnroid Options: I have set the Project Properties (of MyAndroidApp) -> Android Options -> Linker -> Linking to None as Android Linker may ignore some assemblies that are used solely dynamically - again, to no avail.
Question
How can I use a secondary reference within a Xamarin Android project? I might have missed something really stupid so I am open to any suggestions.
There are two different ways of dependency management, just as your guess it. MyPclLibrary project use MyPclLibrary. deps.json to merge compilationOptions from the input project.json, while MyAndroidApp project use the packages.config to copy the reference dll to bin folder directly. So we could not copy the Sqlite DLL from the MyPclLibrary. deps.json to the MyAndroidApp\bin\Debug directory.
The workaround for this issue is that add reference to Microsoft.EntityFrameworkCore.Sqlite directly from MyAndroidApp (Just as you have already tried). The default behavior for Building Android Apps with Entity Framework is add the Sqlite NuGet packages to the Android project.
I have a solution that has a project that has a class that I'd like to share with another project in the same solution.
This class depends on a library that was pulled in from nuget.
Because the second project hasn't pulled this same library from nuget, the class execution fails (despite the second project depending on the first).
Up to this point, I've just been going and adding the same nuget packages to multiple projects.
This seems terribly unwieldy as when I need to update one project, I have to remember to update them all, or depend on tests, which seems like a great opportunity for bugs to slip through the cracks.
How can I go about sharing these dependencies between multiple projects in the same solution?
I create a sample solution based on your description, I found the dependencies nuget library could be found automatically in other projects that reference Class1 project. But if I set the Copy to Local property as false for the nuget library in Class1 project references, I will get an error message about could not find the dependencies when running other projects.
So please check the Copy to Local property for the nuget library in your Class1 project to confirm it is set as True.
I have a WinForms application that references a data model class library in the same solution. This class library uses Entity Framework 5. So far it works great.
I would also like to reference an external class library that uses Entity Framework 6, but at this point, as expected, I get the System.IO.FileLoadException: Could not load file or assembly 'EntityFramework, Version=6.0.0.0...
From what I understood, I have two options (or maybe more):
1) Upgrade or downgrade one of the two, so they both use the same
version of EF (downgrade is probably not a good idea).
2) Make my solution to be able to support both versions (that's my preferred
option)
Now at this point I am getting stuck, because I am not sure how I can add references to EF5 and EF6, as both DLLs are called EntityFramework.dll.
I also saw these questions: Different EntityFramework versions in same solution
Could not load file or assembly 'EntityFramework' after downgrading EF 5.0.0.0 --> 4.3.1.0
But it's still not clear to me exactly how to do it. I can see that I need to add a dependent assembly in my app config.
Just to clarify, this is the project structure that I have:
- Main application
- Data model class library (uses EF5 and it is referenced by the Main application)
- External class library (uses EF6 and it is referenced by the Main application)
Is anybody able to help with that?
So, I need help to configure EF migrations.
I recently connected to a TFS repository and do a 'Get Latest Version' to a project that uses EF migrations. After I get the code I tried to run Add-Migration but VS said that I need to Enable-Migration first and then when I do I got the following error message:
No context type was found in the assembly
Basically, I dont know what to do next. If I got a project from TFS that uses Migration already I can use migrations locally already or I need to enable first? If I need to enable, what I need to do? I tried to point to a project where I have my Web.Config pointing to my database but I don't get any lucky.
for this you should have to enable maigration on your DataAccess project
Enable-Migrations -ProjectName dataAccessProjectName -StartUpProjectName yourWebProject
try to run this command on package manage console
A few checks you can do -
1. Identify your "data project" in the solution. This is project should have a reference to "EntityFramework", a DataContext cs file and a "migration" folder.
3. Build your solution,
4. Set your start-up project to the project which typically has config,
5. Switch to "package manager console" and select the data project under the "default project" dropdown.
6. run "add-migration"