How to Temporarily disable all foreign key constraints

September 12, 2021 0 comments

 While working with MSSQL and Asp.net Core with Entity Framework, many times we have came across a situation where we have to migrate data from one database to newly created database. e.g. Copying all the data from Production DB to Testing DB. 

  • Using Entity Framework Code First approach, we create database
  • Once database is created, need to copy all the data from MSSQL (Generate script > Dataonly); create Database script for the data
  • Now while running the script generated from PROD db to Test DB (there could me some modifications in test db, some columns are added) , we need to disable foreign key constraint and then re-enable foreign key constraints
 

How to disable all the foreign key constraints in MSSQL and then again re-enable all the foreign key constraints in MSSQL 

We can use following scripts

-- Disable all the constraint in database
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'

-- Enable all the constraint in database
EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'

This is the most easiest way to Disable and Enable all the Foreign key constraints on MSSQL.

Mvc Core - Multiple custom attributes of the same type found - new scaffoled item - controller with view

April 27, 2019 0 comments
While working on myAttendance.io project in mvc.net core 2.2, I was creating a company and wanted to generate Controller with Views. My company Class is as follows


 public class Company : BaseModel
    {
        public string Logo { get; set; }

        [DisplayName("Company Name")]
        [Required]
        [MaxLength(150)]
        public string CompanyName { get; set; }

        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail address")]
        [EmailAddress(ErrorMessage = "Please enter valid email address.")]
        [Required]
        [MaxLength(150)]
        public string Email { get; set; }
        [Required]
        [DataType(DataType.Url)]
        [Url(ErrorMessage = "Invalid URL!")]
        [MaxLength(150)]
        public string Web { get; set; }

        [Phone]
        [Required]
        [MaxLength(15)]
        public string Phone { get; set; }

        [Required]
        [MaxLength(10)]
        public string Currency { get; set; }
        [Required]
        [MaxLength(250)]
        public string Address { get; set; }

    }
When I tried adding New Scaffolded item, I got error saying
 There was an error running the selected code generator: 'Multiple custom attributes of the same type found'

Multiple custom attributes of the same type found

To resolve this error I had to remove
[DataType(DataType.Url)]
If we see model then I have already used
[Url(ErrorMessage = "Invalid URL!")]
This same is applicable for followings
[EmailAddress(ErrorMessage = "Please enter valid email address.")]

[Phone(ErrorMessage = "Please enter valide phone number.")]
We have EmailAddress and PhoneNumber as Datatype also
[DataType(DataType.EmailAddress)]

[DataType(DataType.PhoneNumber)]
So Just make sure we have DataType or corresponding Tag to avoid 'Multiple custom attributes of the same type found.'

More than one DbContext named 'myproject.Models.myprojectContext' was found. Specify which one to use by providing its fully qualified name using its exact case

April 15, 2019 0 comments

I was developing a Web-Application in Asp.Net Core 2.2 . After I added the new identity with scaffold it generated me these codes:
For IdentityHostingStartup.cs




[assembly:
 HostingStartup(typeof(myproject.Areas.Identity.IdentityHostingStartup))]
namespace myproject.Areas.Identity
{
    public class IdentityHostingStartup : IHostingStartup
    {
     public void Configure(IWebHostBuilder builder)
      {
      builder.ConfigureServices((context, services) => {
      services.AddDbContext(options =>
       options.UseSqlServer(
       context.Configuration.GetConnectionString("myprojectContextConnection")));

        services.AddDefaultIdentity()
                .AddEntityFrameworkStores();
            });
        }
    }
}

Code in my Startup.cs (After adding Identity I made some modifications in startup.cs, with reference to this article)

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.Configure(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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddRazorPagesOptions(options =>
        {
        options.AllowAreas = true;
        options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
        options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
        });

      services.ConfigureApplicationCookie(options =>
        {
        options.LoginPath = $"/Identity/Account/Login";
        options.LogoutPath = $"/Identity/Account/Logout";
        options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
        });

        services.Configure(Configuration.GetSection("EmailSettings"));
        services.AddSingleton();
        services.AddScoped();

        }

        // 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();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseAuthentication();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }

This was working fine, then I added new models and wanted to scaffold a model with controller and views using Entity Framework. After I set up everything and click ok, I started getting error saying : More than one DbContext named 'myproject.Models.myprojectContext' was found. Specify which one to use by providing its fully qualified name using its exact case.

More than one DbContext was found. Specify which one to use by providing its fully qualified name using its exact case.


 To resolve this issue I removed following lines of code from IdentityHostingStartup.cs

  services.AddDbContext(options =>
   options.UseSqlServer(
   context.Configuration.GetConnectionString("myprojectContextConnection")));

And I modified my startup.cs file to following

 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.Configure(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(options =>
  options.UseSqlServer(Configuration
.GetConnectionString("myprojectContextConnection")));


  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddRazorPagesOptions(options =>
    {
    options.AllowAreas = true;
    options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
    options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
     });

   services.ConfigureApplicationCookie(options =>
    {
    options.LoginPath = $"/Identity/Account/Login";
    options.LogoutPath = $"/Identity/Account/Logout";
    options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
     });

    services.Configure(Configuration.GetSection("EmailSettings"));
    services.AddSingleton();
    services.AddScoped();

    }

        // 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();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change 
                //this for production scenarios,
                // see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseAuthentication();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }

Now I try generating Scaffold items it works without any error.