I keep getting this error when I attempt to save my database changes:
> {"Store update, insert, or delete statement affected an unexpected
> number of rows (0). Entities may have been modified or deleted since
> entities were loaded. See
> http://go.microsoft.com/fwlink/?LinkId=472540 for information on
> understanding and handling optimistic concurrency exceptions."}
The main program routine is:
class Program
{
static void Main( string[] args )
{
var ctx = new SchoolContext();
Student stud = new Student();
stud.StudentID = 0;
stud.Height = 0.2;
stud.StudentName = "New Student";
ctx.Students.Add(stud);
ctx.SaveChanges();
}
}
The DbContext is:
public class SchoolContext : DbContext
{
public SchoolContext() : base("name=SchoolContext")
{
}
public virtual DbSet<Student> Students { get; set; }
}
and the model is:
public class Student
{
public Student()
{
}
public int StudentID { get;set; }
public string StudentName { get;set; }
public double Height { get;set; }
}
My app.config is:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
<connectionStrings>
<add name="SchoolContext" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=test;uid=root;password=tree"/>
</connectionStrings>
</configuration>
My packages.config file is:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net40" />
</packages>
My database table is defined as:
DROP TABLE IF EXISTS `students`;
CREATE TABLE `students` (
`Height` double(10,5) DEFAULT NULL,
`StudentId` int(2) DEFAULT NULL,
`StudentName` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The exception occurs in the main program at the line: ctx.SaveChanges();
There is no additional code that modifies my entries after having been loaded, so this exception makes no sense. The only files in my project are the ones posted above.
Does anyone know why this occurs and how I should fix it?
A code snippet would be very helpful.
Thanks.