EF4 Code First CTP5:种子方法不再有效 [英] EF4 Code First CTP5: Seed method no longer works

查看:141
本文介绍了EF4 Code First CTP5:种子方法不再有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有EF Code First CTP4工作正常,我今天安装了CTP5。现在,当我的数据库被重新填充时,我收到一个例外。



这是我的模型:

  public class Member 
{
public Member()
{
DateCreated = DateTime.Now;
DateUpdated = DateTime.Now;
DateLastLogin = DateTime.Now;
}
[Key,DatabaseGenerated(DatabaseGenerationOption.Identity)] \\我尝试删除这些注释,结果是一样的
public int MemberId {get;组; }
[必需,RegularExpression(。+ \\ @。+ \\ .. ..,ErrorMessage =请输入有效的电子邮件地址)]
public string Email {get ;组; }
[必需,StringLength(20,MinimumLength = 2)]
public string FirstName {get;组; }
[必需,StringLength(20,MinimumLength = 2)]
public string LastName {get;组; }
[必需,StringLength(36,MinimumLength = 2)]
public string City {get;组; }
[必需,StringLength(20,MinimumLength = 2)]
public string State {get;组; }
[必需,StringLength(20,MinimumLength = 2)]
public string Zip {get;组; }
public double GeoLat {get;组; }
public double GeoLong {get;组; }
public string AccountStatus {get;组; }
public DateTime DateCreated {get;私人集合}
public DateTime DateUpdated {get;组; }
public DateTime DateLastLogin {get;组; }

public virtual PublicProfile Profile {get;组; }
}

这是在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 = 活性 ,
State =CA,
FirstName =Jenny,
LastName =Jennerson,
Zip =94123,
GeoLat = 37.735,
GeoLong = -122.392}
};
Members.ForEach(m => context.Members.Add(m));
context.SaveChanges();
}
}

当它在上下文中点击SaveChanges时,我得到这个例外:


检测到冲突的变化。当尝试插入具有相同密钥的多个实体时,可能会发生这种情况。



描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误的更多信息及其在代码中的位置。



异常详细信息:System.Data.UpdateException:检测到冲突更改。有可能在尝试插入具有相同密钥的多个实体时发生。


有没有人知道发生了什么变化,工作了吗?需要改变哪些工作?

解决方案

您的异常的原因是数据类型int与DatabaseGenerationOption.Identity相结合。使用身份将生成一个guid而不是一个int键。



我有一个类似的问题,因为我想要一个类型为int的Key和一个Guid作为一个唯一的rowid。如果我们在一个实体上使用它们,即使它们在不同的道具中,种子也会停止工作。


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?

解决方案

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.

这篇关于EF4 Code First CTP5:种子方法不再有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆