I am working with ASP.Net Core 2.1 & I am facing a problem. I have been working on implementing a Database-First application. The initial Scaffold-DbContext command works just fine and creates all my entities correctly. After that, I make some changes to model files for Validation. My DBA made a new change on DB so I re-Scaffold the DB. Then I notice that re-scaffold overwrites all the custom code I have added to all the model files. Is there any way I re-scaffold the DB but that only changes those files changes by DBA in ASP.Net Core DB First Approach? Every time I am facing this problem. I Re-scaffold with the below command:
Scaffold-DbContext "Server=192.168.46.101;Database=DBNAME;User Id=USERID;Password=PASSWORD" Microsoft.EntityFrameworkCore.SqlServer -ContextDir Data -OutputDir Models -UseDatabaseNames -force
My Custom Added Annotation to Model:
//Custom Annotation
[Key]
public int COLORCODE { get; set; }
//Custom Validation
[Required(ErrorMessage = "Color Name can not be empty")]
public string COLOR { get; set; }
public string REMARKS { get; set; }
After Re-scaffolding my code be like:
public int COLORCODE { get; set; }
public string COLOR { get; set; }
public string REMARKS { get; set; }
My Program.cs File:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
logging.AddNLog();
})
.UseStartup<Startup>();
}
My Startup.cs File:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = new PathString("/Administrator/AccessDenied");
});
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
// options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<Context>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("ConnectionName")));
services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.Password.RequiredLength = 15;
options.Password.RequiredUniqueChars = 5;
options.Password.RequireNonAlphanumeric = false;
options.SignIn.RequireConfirmedEmail = true;
options.Tokens.EmailConfirmationTokenProvider = "CustomEmailConfirmation";
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
})
.AddEntityFrameworkStores<Context>()
.AddDefaultTokenProviders()
.AddTokenProvider<CustomEmailConfirmationTokenProvider<IdentityUser>>("CustomEmailConfirmation");
// REGISTER ExtractEMService
ExtractEMService.ExtractEMRegisterService(services);
//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseStatusCodePagesWithReExecute("/Error/{0}");
//app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Basic}/{action=BasicColors}/{id?}");
});
}
}