TryUpdateModel错误

时间:2013-06-13 10:57:28

标签: asp.net-mvc-3 c#-4.0 entity-framework-4.1

我第一次可以在我的数据库中添加过敏而没有问题,如下面的屏幕。

enter image description here

但是当我尝试添加第二条记录时(在保存第一条记录之后),它会给出下面提到的运行时异常(如下面的屏幕)。

enter image description here

运行时异常

enter image description here

  

保存不公开外键的实体时发生错误   他们关系的属性。 EntityEntries属性将   返回null,因为无法将单个实体标识为源   例外。可以在保存时处理异常   通过在实体类型中公开外键属性更容易。看到   InnerException以获取详细信息。

Stack Trace(这是我试图为医疗表添加第二条记录的时候。但是对于过敏表也是如此)

  

违反PRIMARY KEY约束'PK__Medicati__3214EC0768A0EA12'。   无法在对象'dbo.Medications'中插入重复键。该声明   已被终止。

行动方法

[HttpPost]
public ActionResult EditMedicalInfo(string providerKey, string ownerKey, string petKey)
{
    var pet = Repository.GetPet(ownerKey, petKey);
    if (TryUpdateModel(pet))
    {
        Repository.Save();
    }
    var url = Url.AbsoluteRouteUrl("PetDetail", new { controller = "customers", action = "detail", providerKey = providerKey, ownerKey = ownerKey, petKey = petKey }) + "#medical";
    return Redirect(url);
}

宠物模型

public class Pet {
        public Pet() { Id = Guid.NewGuid(); Created = DateTime.Now; }

        public Guid Id { get; set; }
        public virtual Owner Owner { get; set; }

        [StringLength(50), Required]
        public string Name { get; set; }
        public string Key { get; set; }
        public DateTime Created { get; set; }
        [Display(Name = "Birth Date"), DataType(DataType.Date)]
        public DateTime? BirthDate { get; set; }
        [EnumDataType(typeof(PetType)), UIHint("EnumerationList")]
        [Required]
        public int Type { get; set; }
        [Required]
        public Guid BreedId { get; set; }
        [Display(Name = "Breed"), ForeignKey("BreedId")]
        public virtual Breed Breed { get; set; }
        [EnumDataType(typeof(Gender)), UIHint("EnumerationList")]
        [Required]
        public int? Gender { get; set; }

        public double Weight { get; set; }

        [Display(Name = "License #")]
        public string LicenseNumber { get; set; }
        [Display(Name = "Microchip #")]
        public string MicrochipNumber { get; set; }

        public int? AgeValue { get { return (BirthDate.HasValue) ? (int)(DateTime.Today - BirthDate.Value).TotalDays : default(int?); } }
        public string Age { get { return (BirthDate.HasValue) ? BirthDate.Value.ToAge() : "Unknown"; } }

        public virtual ICollection<PetPhoto> Photos { get; set; }
        public virtual ICollection<Appointment> Appointments { get; set; }
        public virtual ICollection<MedicalRecordOrder> Orders { get; set; }
        public virtual ICollection<PetDocument> Documents { get; set; }
        public virtual ICollection<PetNote> Notes { get; set; }
        public virtual ICollection<PetProvider> Providers { get; set; }
        public virtual ICollection<PetService> PetServices { get; set; }
        public Guid? Avatar { get; set; }
        public virtual MedicalRecord Medical { get; set; }
        public virtual BehavioralRecord Behavioral { get; set; }
        public virtual DietRecord Diet { get; set; }
        public Guid? EmergencyVeterinarianId { get; set; }
        [ForeignKey("EmergencyVeterinarianId")]
        public virtual Provider EmergencyVeterinarian { get; set; }
        public virtual ICollection<PetContact> Contacts { get; set; }
        [EnumDataType(typeof(ProfileCreatorType))]
        public int ProfileCreator { get; set; }

        [EnumDataType(typeof(PetClassification)), UIHint("EnumerationList")]
        public int Classification { get; set; }

        [UIHint("InsuranceCarrier")]
        public virtual string InsuranceCarrier { get; set; }

        // Non persisted extensions
        /// <summary>
        /// Non Persisted
        /// </summary>
        [NotMapped]
        public List<AppointmentInfo> AppointmentInfos { get; set; }
        /// <summary>
        /// Non Persisted
        /// </summary>
        [NotMapped]
        public List<AppointmentInfo> SiblingAppointmentInfos { get; set; }

        public IList<ReservationRequest> ReservationRequests { get; set; }

        [UIHint("QuickList")]
        public virtual ICollection<SpecialInstruction> SpecialInstructions { get; set; }

        public virtual PetSitterRestrictionPermission PetSitterRestrictionPermission { get; set; }
        public virtual PetSitterBehavior PetSitterBehavior { get; set; }
        public virtual PetSitterCleaningRecord PetSitterCleaningRecord { get; set; }
        public virtual PetSitterNote PetSitterNote { get; set; }


    }

过敏模型

public class Allergy {
        public Allergy() { Id = Guid.NewGuid(); }
        [ScaffoldColumn(false)]
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Treatment { get; set; }
    }

当我尝试添加第二条记录时,如何避免上述错误?

0 个答案:

没有答案