开发者

EF4 Code First CTP5: Seed method no longer works [closed]

开发者 https://www.devze.com 2023-01-29 19:26 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about programming within the scope defined in the help center.

Closed 2 years ago.

Improve this 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&quo开发者_如何学Got;)]
    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?


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


The reason for your exception is that you have datatype int combined with DatabaseGenerationOption.Identity. Using identity will generate a guid and not a int key.

I have a similar problem because I want to have a Key of type int and a Guid as a unique rowid. And seed stops working if I use both on a entity even if they're in separate props.

0

精彩评论

暂无评论...
验证码 换一张
取 消