简体   繁体   中英

The INSERT statement conflicted with the FOREIGN KEY constraint in Entity Framework database first

I'm trying to insert data to Database using stored procedure and Entity Framework with a database-first approach.

I'm using ASP.NET MVC to perform the operations.

Below is my model class. Here I'm using country, gender & state table is been FK to my Employee table.

[MetadataType(typeof(EmployeeMetadata))]
public partial class Employee
{
}

public class EmployeeMetadata
{
        public int ID { get; set; }
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        public Nullable<int> Gender { get; set; }
        public Nullable<int> Salary { get; set; }
        public string Designation { get; set; }
        [Display(Name = "Company Name")]
        public string Cmpname { get; set; }
        [Display(Name = "Department Name")]
        public string Dep_type { get; set; }
        public Nullable<int> CountryId { get; set; }
        public Nullable<int> Stateid { get; set; }

        [ForeignKey("Gender")]
        public virtual tblGender tblGender { get; set; }
        [ForeignKey("CountryId")]
        public virtual tblcountry tblcountry { get; set; }
        [ForeignKey("Stateid")]
        public virtual tblstate tblstate { get; set; }
}

[MetadataType(typeof(tblGenderMetadata))]
public partial class tblGender
{
}

public class tblGenderMetadata
{
        public int Genid { get; set; }
        [Display(Name = "Gender")]
        public string GenderName { get; set; }
}

[MetadataType(typeof(tblcountryMetadata))]
public partial class tblcountry
{
}

public class tblcountryMetadata
{
        public int CId { get; set; }
        [Display(Name = "Country Name")]
        public string C_Name { get; set; }
}

[MetadataType(typeof(tblstateMetadata))]
public partial class tblstate
{
}

public class tblstateMetadata
{
        public int sId { get; set; }
        [Display(Name = "State Name")]
        public string state_name { get; set; }
        public Nullable<int> country_code { get; set; }
}

But when I'm trying to execute my insert stored procedure, the application shows me this error:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CountryId". The conflict occurred in database "EmployeeDB", table "dbo.tblcountry", column 'CId'. The statement has been terminated.

This is my stored procedure:

ALTER PROCEDURE [dbo].[ProCRUD_EmpDB]
     @ID int = NULL,
     @FirstName nvarchar(50) = NULL,
     @LastName nvarchar(50) = NULL,
     @Gender int = NULL,
     @Salary int = NULL,
     @CountryId int = NULL,
     @Stateid int = NULL,
     @Designation varchar(50) = NULL,
     @Cmpname varchar(20) = NULL,
     @Dep_type varchar(15) = NULL,
     @Flag varchar(15)
AS
BEGIN
    IF @Flag = 'Select'
    BEGIN
        SELECT 
            ID, FirstName, [LastName],
            Gender,
            CountryId, Stateid,
            [Salary],
            Designation,
            ISNULL ([Cmpname], 'N/A') AS [Cmpname],
            ISNULL ([Dep_type], 'N/A') AS [Dep_type]  
        FROM
            Employees
        WHERE 
            id = @id
    END

    IF @Flag = 'SelectAll'
    BEGIN
        SELECT 
            ID, FirstName, [LastName],
            Gender, GenderName,
            CountryId, Stateid, C.Name, s.state_name,
            [Salary],
            Designation,
            ISNULL ([Cmpname], 'N/A') AS [Cmpname],
            ISNULL ([Dep_type], 'N/A') AS [Dep_type]  
        FROM
            Employees
        LEFT JOIN 
            tblGender g ON Employees.Gender = g.Genid
        LEFT JOIN 
            tblcountry c ON Employees.CountryId = c.CId
        LEFT JOIN 
            tblstate s ON Employees.Stateid = s.sId
    END

    IF @Flag = 'Delete'
    BEGIN
        DELETE FROM Employees 
        WHERE ID = @id
    END

    IF @Flag = 'Gender'
    BEGIN
        SELECT * FROM tblGender
    END

    IF @Flag = 'Country'
    BEGIN
        SELECT * FROM tblcountry
    END

    IF @Flag = 'State'
    BEGIN
        SELECT * 
        FROM tblstate 
        WHERE country_code = @CountryId
    END

    IF @Flag = 'Insert'
    BEGIN
        INSERT INTO [dbo].[Employees] ([FirstName], [LastName], [Gender], Designation,
                                       [Salary], [Cmpname],[Dep_type], 
                                       CountryId, Stateid)
        VALUES (@FirstName, @LastName, @Gender, @Designation,
                @Salary, @Cmpname, @Dep_type, @CountryId, @Stateid)
    END

    IF @Flag = 'Update'
    BEGIN
        UPDATE [dbo].[Employees]
        SET [FirstName] = @FirstName,
            [LastName] = @LastName,
            [Gender] = @Gender,
            [Salary] = @Salary,
            [Cmpname] = @Cmpname,
            Designation = @Designation,
            [Dep_type] = @Dep_type,
            CountryId = @CountryId,
            Stateid = @Stateid
        WHERE
            ID = @id
    END 
END

Also, I've tried to add a foreign key to the virtual table still show me the above error.

I think the issue is the country id you are trying to insert in employee table is not equal to the country id added in country table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM