Entity Framework code first INSERT statement conflicted with the FOREIGN KEY constraint

c# entity-framework entity-framework-6 sql

Question

I am using EF6 code first with Sqlserver 2008 R2.

Below are my Model classes

  public class Ninja
    {
        public int Id { get; set; }
        public string  Name { get; set; }
        public bool ServedInOniwaban { get; set; }
        public Clan Clan { get; set; }
        public int ClanId { get; set; }
        public List<NinjaEquipment> EquipmentOwned { get; set; }
        public System.DateTime  DateOfBirth { get; set; }
    }
    public class NinjaEquipment
    {
        public int Id { get; set; }
        public string  ClanName { get; set; }
        public EquipmentType Type { get; set; }
        [Required]
        public Ninja Ninja { get; set; }

    }
    public class Clan
    {
        public int Id { get; set; }
        public string ClanName { get; set; }
        public List<Ninja> Ninjas { get; set; }

    }

And DbContext class as below,

 public class NinjaContext: DbContext 
    {
        public DbSet<Ninja> Ninjas { get; set; }
        public DbSet<Clan> Clans{ get; set; }
        public DbSet<NinjaEquipment> Equipment { get; set; }
    }

Here is my Program class,

 class Program
    {
        static void Main(string[] args)
        {
            InsertNinja();
            Console.ReadKey();


        }

        private static void InsertNinja()
        {
            var ninja = new Ninja
            {
                Name = "JulieSan",
                ServedInOniwaban = false,
                DateOfBirth = new DateTime(1980,1,1),
                ClanId = 1,
            };
            using (var context= new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                context.Ninjas.Add(ninja);
                context.SaveChanges();

            }
        }
    }

Before running the code I have inserted values in Clan table. Clan table has one row present as below 1, 'ClanName'. Now , while running the code in my program class I am trying to insert row in table called Ninja, as Ninja table has foreign key as ClanId , I am providing clanId as 1 , So it should not throw Foreign key constraint.

When I tried to run this program I got below error, {"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_dbo.Ninjas_dbo.Clans_ClanId\". The conflict occurred in database \"NinjaDomain.DataModel.NinjaContext\", table \"dbo.Clans\", column 'Id'.\r\nThe statement has been terminated."}

On other hand when I tried running below sql script against db I am able run it successfully,

INSERT [dbo].[Ninjas]([Name], [ServedInOniwaban], [ClanId], [DateOfBirth])
VALUES ('JulieSan', 'False', '1', '01-01-1980 00:00:00' )
SELECT [Id]
FROM [dbo].[Ninjas]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()
1
0
7/16/2016 9:03:47 PM

Accepted Answer

Actually it was issue with th db server name. I was using db with Name : (localdb)\mssqllocaldb in EF adapters So all db schema was created in EF adapter project. Then I have added Console application and installed EF in that presentation project.After adding EF in presentation project (Console App) it by default created App.Config, SO I have removed app.config from EF Adapter project. So app.config of console app was pointing to .\SqlExpress db instead of (localdb)\mssqllocaldb. My bad.

0
7/17/2016 8:52:11 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow