Question: Is there a way to get dotnet ef dbcontext scaffold to not only put the context file in a different directory but include the entity namespace in that file?
I'm using dotnet ef dbcontext scaffold to generate an EF context and related entities from a database. The database design is handled separately, so regenerating the entities and context periodically will be required.
I'm using this, which works fine:
dotnet ef dbcontext scaffold `
"Server=localhost;Database=My.Db;Trusted_Connection=True;" `
Microsoft.EntityFrameworkCore.SqlServer `
--startup-project My.EfCliStartupProject\My.EfCliStartupProject.csproj `
--force `
--project My.Orm\My.Orm.csproj `
--context MyContext `
--schema MY_SCHEMA `
--output-dir Entities
I'd like to be able to put the scaffolded context one level up by adding --context-dir .. Unfortunately, there's no using statement at the top of the context file referencing the entity namespace.
This command is in a quick PowerShell script to do this that also clears out the entity directory, so it wouldn't be impossible to to add some code to that script to append the using statement, but that's not ideal.
I'm using EF CLI 3.0.0. Trying to install later versions gets me an error: error NU1202: Package dotnet-ef 3.1.2 is not compatible with netcoreapp3.0.
There is an older question for Scaffold-DbContext with a mix of answers, but it's not clear if the using statement is expected to be missing or not.
Thanks!
This feature was included in version 3.1 of the tool, which I suppose is why you were having this problem on 3.0.0.
https://github.com/dotnet/efcore/pull/18405
Related
I have got some work to modify default html generated by Jhipster for Angular2 more specifically,i want to modify styles for grid generated by Jhipster for Entity's.
I have gone through tutorial creating module in Jhispter documentation.
https://jhipster.github.io/modules/creating-a-module/
generator for creating subgenerator is also their can someone please give me pointers on this problem?
e.g
yo jhipster:entity Author
from above command jhipster creates all necessary files at server and client like JPA Entity,REST controller,Service, at client side it creates CRUD UI for Author entity using Angular2(i opted for anular2)
I want to modify client generation Code, i just want to modify HTML templates which gets generated by jhipster entity sub:generator
you need to run Yarn and start modifying your author.component.html
which will automatically refreshed in your Browser.
https://jhipster.github.io/development/
You could also consider using git prior to running the generator and then reverting the server side code once the generator has ran
For doing authorization in asp.net mvc 5 I have searched lot on internet but not getting correct answer. Actually I am using Visual studio community edition with .net framework 4.5.2 and mvc5. I want to do the Authorization in my project. My project is created with no authentication mode while creating a project.
Firstly I have tried these things:
I typed a command "Enable Migrations" on Package manager console.
Then I got one error like: Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
I dont know how to resolve this error. please help me to get out of it.
Which approach did you chooses to build our database model? Code First, Model First or Database First?
I'm asking because you can't use migrations with Model First or Database First approach. EF Migrations is a product targeted for Code First approach. CodeFirst assumes that you will never make any changes manually to the database. All the changes to the database will go through the code first migrations.
But there is solution for that - you can use Code First approach with an existing database. Take a look here: Using EF “Code First” with an Existing Database.
Im develop the MVC project, i have a some issue for this , im already to created database , but i cant update DB in add PM> Update-Database method,
displayed the following error , how can i fix it?
No migrations configuration type was found in the assembly 'MVCMovei'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
Thank you
In the package manager console run the following command
Enable-Migrations
After that run Command
Add-Migrations "AnyName" (initialmigration etc)
After that Run
Update-Database (if you want to make changes in database)
I came across this issue whilst working on MVC Movie App tutorial too. Using MS Visual Studio Nuget Packet Manager(PM) do the following;
PM> Enable-Migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project MVC Movie.
PM> Add-Migration Initial
Scaffolding migration 'Initial'.
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201808240707259_Initial].
Applying explicit migration: 201808240707259_Initial.
Running Seed method.
To follow these solution in detail use these resource from MS Docs. I hope this help other starters on SO who get stuck.
Due to some reasons (too long story) I can't use automatic code first migrations. But I would need to be able to create SQL script for creating tables (any other way with the same result is good as well) in my database based on the model, without writing the Seed method? Sometimes I don't need any seeding, just creating tables would suit my needs as I have to test creating records anyway. But I cant't find a way to do. Any suggestion?
You can use migrations to generate a script with -script rather than updating the database directly with update-database.
PM> Enable-Migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project WebApplication10.
PM> Add-Migration Initial
Scaffolding migration 'Initial'.
PM> Update-Database -TargetMigration Initial -Script
Applying explicit migrations: [201404151642086_Initial].
Applying explicit migration: 201404151642086_Initial.
PM>
At this point you should see the script. I don't think it includes any seed data. If it does, just remove it before running the script.
I have an existing project using RIAServices with Entity Framework. The project builds correctly and generates the AmsiWeb.g.cs file with all the context classes for my services.
I am converting my designer based entities and ObjectContext with Code First entities and DbContext. I installed the RIAServices.EntityFramework NuGet package to the web application that contains my services. However, now when I build the AmsiWeb.g.cs file only contains the WebContext class. It doesn't contain any generated services.
I have only at this point converted a single EDMX model to Code First and DbContext and made the requisite changes to the services that use that model to inherit from DbDomainService.
I am using EF 5.0... not sure if that matters cause I'm not sure how adding a DLL to the AmsiWeb application project would break code generation.
What would cause this to no longer work and how can I fix it?
Maybe it's a problem within the msbuild task that generates the proxy code (I mean the *.g.cs file). Probably it's looking for the wrong version of a entity framework. Have a look at this blog post http://mcasamento.blogspot.it/2012/10/entity-framework-5-code-first-and-wcf.html in the final part I wrote an assembly redirect statement that did the trick
It turns out that their needs to be a redirect for Entity Framework 5.0 (4.4.0.0 since I was using .Net 4.0) in the web.config. But, since my RIA Services were in a web application project that was not my root project the code wasn't generating.
Once I added the redirect to the web.config of the web application with the RIA services in it, the context code was correctly generated.