Entity Framework Error:
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Migrations.IMigrator'. This is often because no database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
Solution:
Verify the connectionstring in the appsettings.json file.
{
"ConnectionStrings": {
"CustomerDb": "Server=(localdb)\\mssqllocaldb;Database=Customer;Trusted_Connection=True;"
}
}
On Startup.cs, ConfigureService method, verify, you are correctly initializing the Context
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
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<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("CustomerDb")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
How To: Unobtrusive Javascript - Manually Add/Remove Required Rule and Message.
<script type="text/javascript">
$(function () {
var addRequired = function () {
// Manually add required fields to certain fields
$('#scheduledLockDate').rules('add', {
required: true,
messages: {
required: "The Scheduled Lock Date is required"
}
});
$('#scheduledDisposeDate').rules('add', {
required: true,
messages: {
required: "The Scheduled Dispose Date is required"
}
});
$('#issueDate').rules('add', {
required: true,
messages: {
required: "The Issue Date is required"
}
});
}
// This will remove the manually required fields
var removeRequired = function () {
// Manually remove required fields to certain controls
$('#scheduledLockDate').rules('remove', 'required');
$('#scheduledDisposeDate').rules('remove', 'required');
$('#issueDate').rules('remove', 'required');
}
// Handle the form submit method
$('#workspaceForm').submit(function (e) {
e.preventDefault();
var form = this;
// Trigger Form Validation
var isValid = $('#workspaceForm').valid();
if (!isValid) {
// Exit out
return false;
}
form.submit();
});
//Manually add the required fields
addRequired();
//Manually remvoe the required fields
removeRequired();
});
</script>
Reference: https://jqueryvalidation.org/rules/