简体   繁体   中英

Inserting data in entity Framework

I'm already stuck for half day figuring what I'm done wrong here. I'm inserting new record with employee and CHRIS info attach here.

My Model:

 public class tblEmployeeDetail
{
    [Key]
    public string EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    private string _Bank;
    public string Bank
    {
        get
        {
            return _Bank;
        }
        set
        {
            _Bank = value.ToString().Replace("-", "");
        }
    }
}
public class tblCHRISEmployee
    {
        [Key]
        public string CHRISID { get; set; }
        public string EmployeeID { get; set; }
        public string Fullname { get; set; }
        private string _Bank;
        public string Bank
        {
            get
            {
                return _Bank;
            }
            set
            {
                _Bank = value.ToString().Replace("-", "");
            }
        }
    }
public class tblWeeklyCredit
{
    [Key]
    public string WeeklyCreditID { get; set; }
    public tblEmployeeDetail Employee { get; set; }
    public string Fullname { get; set; }
    public string ReportID { get; set; }
    public float Amount { get; set; }
    public string Description { get; set; }
    public DateTime UploadDate { get; set; }
}

my is my function to inserting new record in the database.

public void AddWeeklyCredit(List<tblWeeklyCredit> WeeklyCreditData)
    {
        using (DatabaseContext db = new DatabaseContext())
        {
            foreach (tblWeeklyCredit RawData in WeeklyCreditData)
            {
                if(RawData==null)
                    throw new Exception(string.Format("{0} null", RawData.ReportID));

                //attach the data
                db.Entry(db.tblCHRISEmployee.Where(x=>x.CHRISID==RawData.CHRIS.CHRISID).FirstOrDefault()).State = EntityState.Unchanged;
                db.Entry(db.tblEmployeeDetail.Where(x => x.EmployeeID == RawData.Employee.EmployeeID).FirstOrDefault()).State = EntityState.Unchanged;

                //validate if the report ID is already Claimed
                if (db.tblWeeklyCredit.Where(x => x.ReportID == RawData.ReportID).FirstOrDefault() != null)
                    throw new Exception(string.Format("{0} is already Claimed", RawData.ReportID));

                db.tblWeeklyCredit.Add
                (
                    RawData
                );
            }

            db.SaveChanges();
        }
    }

I am getting this error:

Constraint failed UNIQUE constraint failed: tblCHRISEmployee.CHRISID

Why have I gotten this error?

I Think -- FirstOrDefault() method returns an element from the zeroth index in the collection... so, You may set id to be the primary key on the list table, which means that value must be unique for each record. Trying to insert multiple records with the same id is causing the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM