0

我第一次可以毫无问题地将过敏症添加到我的数据库中,如下图所示。

在此处输入图像描述

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

在此处输入图像描述

运行时异常

在此处输入图像描述

保存不为其关系公开外键属性的实体时发生错误。EntityEntries 属性将返回 null,因为无法将单个实体标识为异常源。通过在实体类型中公开外键属性,可以更轻松地在保存时处理异常。有关详细信息,请参阅 InnerException。

堆栈跟踪(这是为了当我尝试为医疗表添加第二条记录时。但过敏表也一样)

违反主键约束“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; }
    }

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

4

0 回答 0