Thursday, May 21, 2020

Delete the foreign key data when a primary key is deleted

According to your description, I create a similar as below, it works well. I couldn't reproduce your issue on my side.

#models

 [Table("Blog")]
    public partial class Blog
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Blog()
        {
            Posts = new HashSet<Post>();
        }

        public int BlogId { get; set; }

        [Required]
        public string Url { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Post> Posts { get; set; }
    }


[Table("Post")]
    public partial class Post
    {
        public int PostId { get; set; }

        public int BlogId { get; set; }

        public string Content { get; set; }

        public string Title { get; set; }

        public virtual Blog Blog { get; set; }
    }

#Dbcontext

namespace IncludeDeleteDemo
{
    using System.Data.Entity;

    public partial class BlogContext : DbContext
    {
        public BlogContext()
            : base("name=BlogContext")
        {
        }

        public virtual DbSet<Blog> Blogs { get; set; }
        public virtual DbSet<Post> Posts { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
    }
}

#Usage:

using (var db = new BlogContext())
            {
                Blog blog = db.Blogs.Include("Posts")
                    .Where(i => i.BlogId == 1003).Single();

                db.Blogs.Remove(blog);
                db.SaveChanges();            
            }

In addition, please try to use Code First Cascade Delete, add the Fluent API, please modify related model name as your requirement.

modelBuilder.Entity<Course>() 
    .HasRequired(t => t.Department) 
    .WithMany(t => t.Courses) 
    .HasForeignKey(d => d.DepartmentID) 
    .WillCascadeOnDelete(true);

Saturday, May 16, 2020

How To Host Your ASP.NET MVC Website On GoDaddy Server

Introduction


In this article, I will explain to you how to host your website to GoDaddy Server. I will also explain how you can create a database server on GoDaddy. But the process we would follow will not be only for GoDaddy, it can be followed for all other hosting services.
  
Note
To host your ASP.NET MVC website on GoDaddy, you must have Windows plan.
 
Let's start step by step.

Step 1 
 
Go to GoDaddy and login with your account credentials.

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 2 
 
Now, your Account page will open where you will get WEB HOSTING. Click on Manage button.

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 3

After that, you will get your hosting server page. From there you can add new domain for a new website or you can also add new sub domain for a new website.

How To Host Your ASP.NET MVC Website On GoDaddy Server

Step 4

Now either Add New Domain or Add Sub Domain. I am going to add new sub domain. When you will add new sub domain you will get the following page.

How To Host Your ASP.NET MVC Website On GoDaddy Server

Step 5 

After adding sub domain, your website will be live and that will look like as follows,

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 6

After giving the name of the website and folder name click on OK button. So it will create a folder for your website which is given in Document Root textbox. Your folder will be like as follows.

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 7

Now create an FTP user for your account by clicking on FTP Access.

How To Host Your ASP.NET MVC Website On GoDaddy Server

Step 8

Now click on Add FTP Account.

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 9

Now, fill the following form after clicking "Add New FTP Account" button. 

How To Host Your ASP.NET MVC Website On GoDaddy Server

Step 10

After completing this process, go back to your Server home page and create database on the server if you have in your project. 

How To Host Your ASP.NET MVC Website On GoDaddy Server

Step 11

Now, click on Add New Database Button.

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 12

Fill the following form to create the database on the Server.

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 13

After completing this process, let's flip to Visual Studio and open the web application which you want to host. In this article I am hosting my old article  "jTable CRUD Operation using MVC". To host your web application, first of all right click on the project and click on the Publish.

How To Host Your ASP.NET MVC Website On GoDaddy Server

Step 14

After clicking on publish button, one dialog box will open for web publishing. First of all, you have to create new profile.

How To Host Your ASP.NET MVC Website On GoDaddy Server

Give any name for your profile and click on Next button.
 
Step 15

Now, make the connection with your FTP server, provide user name password for your FTP server and Validate Connection.

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 16

After validation, click on Next button and apply the following settings.


Step 17

Now, click on Publish button and wait for publishing.
 
Step 18

After publishing the website, flip to GoDaddy server page again and click on ASP.NET Settings.

How To Host Your ASP.NET MVC Website On GoDaddy Server

Step 19

Write Connection String for your server side database. 

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 20

Open Visual Studio again and update database from package manager, again writing same connection string in web.config of your project in Visual Studio. Open package manager console and type the following command,
  1. pm>update-database   
How To Host Your ASP.NET MVC Website On GoDaddy Server

Step 21

Now, quickly open your website by my website URL that is "http://c-sharpcorner.sourabhsomani.com/"When you will run website you will get the following error,

How To Host Your ASP.NET MVC Website On GoDaddy Server

Note

This is the normal error which will come every time when you will host your website. To fix this error you have to modify your web.config on server side.
 
Step 22

To fix above error open your website directory on server. To open that click on File Manager.

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 23

Click on Web.Config,

How To Host Your ASP.NET MVC Website On GoDaddy Server
 
Step 24

Now click on Edit in Text Editor button which will be bottom of the Text Area.

How To Host Your ASP.NET MVC Website On GoDaddy Server

Step 25

Now add two lines in web.config. That is as follows:

How To Host Your ASP.NET MVC Website On GoDaddy Server

Step 26

Now finally, run your website with your domain. I am surfing by the following URL.
 
Output

How To Host Your ASP.NET MVC Website On GoDaddy Server

Wednesday, May 13, 2020

What is a synonym in SQL Server

In SQL Server, a synonym is an alias or alternative name for a database object such as a table, viewstored procedureuser-defined function, and sequence. A synonym provides you with many benefits if you use it properly.

A synonym is a database object that serves the following purposes:

  • Provides an alternative name for another database object, referred to as the base object, that can exist on a local or remote server.

  • Provides a layer of abstraction that protects a client application from changes made to the name or location of the base object.

For example, consider the Employee table of Adventure Works, located on a server named Server1. To reference this table from another server, Server2, a client application would have to use the four-part name Server1.AdventureWorks.Person.Employee. Also, if the location of the table were to change, for example, to another server, the client application would have to be modified to reflect that change.

To address both these issues, you can create a synonym, EmpTable, on Server2 for the Employee table on Server1. Now, the client application only has to use the single-part name, EmpTable, to reference the Employee table. Also, if the location of the Employee table changes, you will have to modify the synonym, EmpTable, to point to the new location of the Employee table. Because there is no ALTER SYNONYM statement, you first have to drop the synonym, EmpTable, and then re-create the synonym with the same name, but point the synonym to the new location of Employee.


SQL Server CREATE SYNONYM statement syntax

To create a synonym, you use the CREATE SYNONYM statement as follows:

CREATE SYNONYM [ schema_name_1. ] synonym_name FOR object;

The object is in the following form:

[ server_name.[ database_name ] . [ schema_name_2 ]. object_name

In this syntax:

  • First, specify the target object that you want to assign a synonym in the FOR clause
  • Second, provide the name of the synonym after the CREATE SYNONYM keywords

Note that the object for which you create the synonym does not have to exist at the time the synonym is created.

SQL Server CREATE SYNONYM statement examples

Let’s take some examples of using the CREATE SYNONYM statement to get a better understanding.

A) Creating a synonym within the same database example

The following example uses the CREATE SYNONYM statement to create a synonym for the sales.orders table:

CREATE SYNONYM orders FOR sales.orders;

Once the orders synonym is created, you can reference it in anywhere which you use the target object (sales.orders table).

For example, the following query uses the orders synonym instead of sales.orders table:

SELECT * FROM orders;

B) Creating a synonym for a table in another database

First, create a new database named test and set the current database to test:

CREATE DATABASE test; GO USE test; GO

Next, create a new schema named purchasing inside the test database:

CREATE SCHEMA purchasing; GO

Then, create a new table in the purchasing schema of the test database:

CREATE TABLE purchasing.suppliers ( supplier_id INT PRIMARY KEY IDENTITY, supplier_name NVARCHAR(100) NOT NULL );

After that, from the BikeStores database, create a synonym for the purchasing.suppliers table in the test database:

CREATE SYNONYM suppliers FOR test.purchasing.suppliers;

Finally, from the BikeStores database, refer to the test.purchasing.suppliers table using the suppliers synonym:

SELECT * FROM suppliers;

Listing all synonyms of a database

You can view all synonyms of a database by using Transact-SQL and SQL Server Management Studio.

A) Listing synonyms using Transact-SQL command

To list all synonyms of the current database, you query from the sys.synonyms catalog view as shown in the following query:

SELECT name, base_object_name, type FROM sys.synonyms ORDER BY name;

Here is the output:

SQL Server Synonym Example

B) Listing synonyms using SQL Server Management Studio

From the SQL Server Management Studio, you can view all synonym of the current database via Synonyms node as shown in the following picture:

SQL Server Synonym using SSMS

Removing a synonym

To remove a synonym, you use the DROP SYNONYM statement with the following syntax:

DROP SYNONYM [ IF EXISTS ] [schema.] synonym_name

In this syntax:

  • First, specify the synonym name that you want to remove after the DROP SYNONYM keywords.
  • Second, use the IF EXISTS to conditionally drop the synonym only if it exists. Removing a non-existing synonym without the IF EXISTS option will result in an error.

Removing synonyms example

The following example uses the DROP SYNONYM statement to drop the orders synonym:

DROP SYNONYM IF EXISTS orders;

When to use synonyms

You will find some situations which you can effectively use synonyms.

1) Simplify object names

If you refer to an object from another database (even from a remote server), you can create a synonym in your database and reference to this object as it is in your database.

2) Enable seamless object name changes

When you want to rename a table or any other object such as a view, stored procedure, user-defined function, or a sequence, the existing database objects that reference to this table need to be manually modified to reflect the new name. In addition, all current applications that use this table need to be changed and possibly to be recompiled. To avoid all of these hard work, you can rename the table and create a synonym for it to keep existing applications function properly.

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...