该物业' Id'是对象的关键信息的一部分,在第二次插入后无法修改

时间:2017-09-29 09:15:02

标签: entity-framework c#-4.0

我有一张Id为主键的表

EF生成的模型是

public partial class Home_Orchard_Users_UserPartRecord
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string NormalizedUserName { get; set; }
    public string Password { get; set; }
    public string PasswordFormat { get; set; }
    public string HashAlgorithm { get; set; }
    public string PasswordSalt { get; set; }
    public string RegistrationStatus { get; set; }
    public string EmailStatus { get; set; }
    public string EmailChallengeToken { get; set; }
    public Nullable<System.DateTime> CreatedUtc { get; set; }
    public Nullable<System.DateTime> LastLoginUtc { get; set; }
    public Nullable<System.DateTime> LastLogoutUtc { get; set; }
}

当我尝试从列表中插入数据时,第一个插入是正常的,但第二个插入错误:属性&#39; Id&#39;是对象关键信息的一部分,不能修改。

      foreach (var ContentItem in ListContentItem)
                {



                    ContentItemData.Data = ContentItem.Data;
                    ContentItemData.ContentType_id = 3;
                    Context.Home_Orchard_Framework_ContentItemRecord.Add(ContentItemData);
                    Context.SaveChanges();
                    int Return_Id = ContentItemData.Id;

                    UserData.Id = Return_Id;
                    UserData.UserName = ContentItem.UserName;
                    UserData.Email = ContentItem.Email;
                    UserData.NormalizedUserName = ContentItem.NormalizedUserName;
                    UserData.Password = "AMQ6a4CzqeyLbWqrd7EwoaTV23fopf++3FpcBlV+Gsvgja3Ye3LwFlXVHyhAcWyKaw==";
                    UserData.PasswordFormat = "Hashed";
                    UserData.HashAlgorithm = "PBKDF2";
                    UserData.PasswordSalt = "FaED9QsIV9HD95m8FO5OSA==";
                    UserData.RegistrationStatus = "Approved";
                    UserData.EmailStatus = "Approved";
                    UserData.CreatedUtc = DateTime.Today;
                    Context.Home_Orchard_Users_UserPartRecord.Add(UserData);
                    //Context.SaveChanges();

                    i = i + 1;

                    progress = ((float)i / nCount) * 100;
                    Console.Write("\r{0}%", progress);
                    Thread.Sleep(50);
                }

为什么我不能第二次插入? Id是主键,它不是自动生成的,因此必须手动输入。

0 个答案:

没有答案