Seed technique no longer works in EF4 Code First CTP5

c# ef-code-first entity-framework

Question

EF Code First CTP4 was running well when I installed CTP5 today. Now when my database is repopulated, I receive an error.

This is my example:

public class Member
{
    public Member()
    {
        DateCreated = DateTime.Now;
        DateUpdated = DateTime.Now;
        DateLastLogin = DateTime.Now;
    }
    [Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]  \\I have tried removing these annotations and the result is the same
    public int MemberId { get; set; }
    [Required,RegularExpression(".+\\@.+\\..+", ErrorMessage = "Please enter a valid email address")]
    public string Email { get; set; }
    [Required,StringLength(20,MinimumLength=2)]
    public string FirstName { get; set; }
    [Required, StringLength(20, MinimumLength = 2)]
    public string LastName { get; set; }
    [Required, StringLength(36, MinimumLength = 2)]
    public string City { get; set; }
    [Required, StringLength(20, MinimumLength = 2)]
    public string State { get; set; }
    [Required, StringLength(20, MinimumLength = 2)]
    public string Zip { get; set; }
    public double GeoLat { get; set; }
    public double GeoLong { get; set; }
    public string AccountStatus { get; set; }
    public DateTime DateCreated { get; private set; }
    public DateTime DateUpdated { get; set; }
    public DateTime DateLastLogin { get; set; }

    public virtual PublicProfile Profile { get; set; }        
}

This is the code that Global.asax.cs is calling.

protected void Application_Start()
{           
     RegisterRoutes(RouteTable.Routes);
     DbDatabase.DefaultConnectionFactory = new SqlConnectionFactory("System.Data.SqlClient");
     DbDatabase.SetInitializer<MeetPplDB>(new Initializer());
}

public class Initializer : DropCreateDatabaseAlways<MeetPplDB>
{
    protected override void Seed(MeetPplDB context)
    {
        var Members = new List<Member>
        {
            new Member {
                Email = "dave@dave.com",
                City = "San Francisco",
                AccountStatus = "Active",
                State = "CA",
                FirstName = "David",
                LastName = "Daverson",
                Zip = "94118",
                GeoLat = 37.735,
                GeoLong = -122.392 },

            new Member { 
                Email = "bob@bob.com", 
                City = "Oakland", 
                AccountStatus = "Active", 
                State = "CA", 
                FirstName = "Bob", 
                LastName = "Boberson", 
                Zip = "94601",
                GeoLat = 37.781,
                GeoLong = -122.216 },

           new Member {
               Email = "jenny@jenny.com",
               City = "San Francisco",
               AccountStatus = "Active",
               State = "CA",
               FirstName = "Jenny",
               LastName = "Jennerson",
               Zip = "94123",
               GeoLat = 37.735,
               GeoLong = -122.392 }
        };
        Members.ForEach(m => context.Members.Add(m));
        context.SaveChanges();
     }
}

I get the following exception when it reaches the SaveChanges on the context:

Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.UpdateException: Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.

Does anyone know what has changed and why this no longer works? What has to be changed for this to function?

1
5
12/15/2014 5:34:56 PM

Popular Answer

Perhaps as a result of EF's creation of your variable name as a plural.

in place of

public class Initializer : DropCreateDatabaseAlways<MeetPplDB>
{
    protected override void Seed(MeetPplDB context)
    {
        var Members = new List<Member>
        {
            new Member {
                Email = "dave@dave.com",
                City = "San Francisco",
.....

Try it.

public class Initializer : DropCreateDatabaseAlways<MeetPplDB>
{
    protected override void Seed(MeetPplDB context)
    {
        var _members = new List<Member>
        {
            new Member {
                Email = "dave@dave.com",
                City = "San Francisco",

....


_members.ForEach(m => context.Members.Add(m));
        context.SaveChanges();

Your code and Scott Gu's http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx are put side by side.

0
9/13/2011 4:55:55 PM


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