How can I keep custom data annotations/fluent api when I scaffold -force in ef core 2.0?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How can I keep custom data annotations/fluent api when I scaffold -force in ef core 2.0?



I'm using EF core 2 with existing database.
Used Scaffold-DbContext command to reverse engineer classes. This worked fine.
Even Fluent API has pulled through correct database field constraints etc. e.g.


. . . .
modelBuilder.Entity<FsReport>(entity =>
{
entity.Property(e => e.Bcclist)
.IsUnicode(false)
.HasDefaultValueSql("('')");

entity.Property(e => e.Cclist)
.IsUnicode(false)
.HasDefaultValueSql("('')");
. . . .



Problem is for 1 field I need to add a custom data annotation. If I add this, then as soon as I rerun Scaffold-DbContext -force command (e.g. if database schema change) then the annotation will be overwritten and removed.



Is there anything I can do to avoid this? I tried putting it in a different partial class of the same name but got a 'The type already contains a definition for.... error'



Many thanks for any help.





If you've used Scaffold-DbContext to create your database in code, then I would move to use Code First. When you use code first and need to deploy the database you can run Update-Database (PS there is equiv for dotnet) command it will create you that database. You can then export that database from SQL to give you the script to run. The benefits of using code first is the ability to use these migrations for when your model changes 6 months down the line and everything is in production.
– Callum Linington
Feb 7 at 9:25







Here are the docs on the commands you can use
– Callum Linington
Feb 7 at 9:28





Thanks @CallumLinington, but the database is already in existance so surely I can't do code first to generate it? This was the whole problem. I used Scaffold-DbContext to create the context and POCO classes related to the database tables. Similar to how used to do it using edmx. I thought code-first was only for new databases e.g. where you want to generate the database from the code? Thanks
– Blingers
Feb 7 at 19:38





If it is already in existence you don't need to generate it... you can still use code first without the edmx and just have the DbContext point at the database
– Callum Linington
Feb 8 at 13:27




2 Answers
2



No. If you insist on sticking with running Scaffold-DbContext (Database first) you will need to add your change back in afterwards. The best way to go forward is to read up on and implement Entity Framework Core Migrations (Code first).


Scaffold-DbContext



I have a similiar situation where I wanted to re-generate the scaffold based on "outside-my-control" DB changes and maintain my custom DbContext edits. I solved this by sub-classing the generated DbContext (referring to it here as ScaffoldDbContext). In the subclass (called ScaffoldNavigationDbContext) I call the base.OnModelCreating before making my custom edits on the modelBuilder.


DbContext


DbContext


ScaffoldDbContext


ScaffoldNavigationDbContext


base.OnModelCreating


modelBuilder



The scaffolding code generates the entity classes as partial so I take advantage and introduce those navigation properties in my classes....separate from the scaffold generated classes. For example, below is my code that will 'survive' a new scaffold generation. There may be caveat's to this but I'm unaware of them and for my simple use-case, it works.


public partial class ScaffoldNavigationDbContext : ScaffoldDbContext {

public ScaffoldNavigationDbContext (DbContextOptions options) : base(options) { }

protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<CertMaster>(entity => {
entity.HasMany(x => x.CertTrans).WithOne(x => x.CertMaster).HasPrincipalKey(x => x.CertId);
});
}
}

public partial class CertMaster {
public List<CertTrans> CertTrans{ get; set; }
}

public partial class CertTrans {
public CertMaster CertMaster { get; set; }
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived