我的EF Code First CTP4工作正常,我今天安装了CTP5。现在,我重新填充数据库时出现异常。
这是我的模特:
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; }
}
以下是在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();
}
}
当它遇到上下文中的SaveChanges时,我得到了这个例外:
检测到冲突的更改。尝试使用相同的密钥插入多个实体时可能会发生这种情况。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。
异常详细信息:System.Data.UpdateException:检测到冲突的更改。尝试使用相同的密钥插入多个实体时可能会发生这种情况。
有没有人知道发生了什么变化以及为什么这不再起作用了?需要改变什么才能发挥作用?
答案 0 :(得分:0)
您的异常的原因是您将数据类型int与DatabaseGenerationOption.Identity结合使用。使用identity将生成一个guid,而不是一个int键。
我有类似的问题,因为我想要一个类型为int的键和一个Guid作为唯一的rowid。如果我在一个实体上使用它们,即使它们在不同的道具中,种子也会停止工作。
答案 1 :(得分:0)
可能是因为您使用的变量名已被EF生成为多元化。
而不是:
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",
.....
试试这个
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();
我将您的代码与Scott Gu的代码进行比较 http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx