Seed technique no longer works in EF4 Code First CTP5

c# ef-code-first entity-framework

Question

I had EF Code First CTP4 working fine, and I installed CTP5 today. Now, I get an exception when my database is repopulated.

Here is my model:

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; }        
}

Here is the code that is being called in Global.asax.cs

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();
     }
}

When it hits the SaveChanges on the context, I get this exception:

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 have any idea what has changed and why this does not work any more? What needs to change for this to work?

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

Popular Answer

Maybe because your variable name that you using has been generated as pluralized by EF.

Instead 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 this

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();

I compare your code with Scott Gu's http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

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