project.Models.tableName:The field pvid must be a

2019-03-03 18:11发布

问题:

I encounter exception error below when i try to add a new entry in the database with :

db.patient_visit.Add(pv);
db.SaveChanges();

An exception of type 'System.InvalidOperationException' occurred in project.dll but was not handled in user code

Additional information: project.Models.tableName:The field pvid must be a string or array type with a maximum length of '20'.

Before this, my model is automatically and originally set like below because i did Code-First migration with existing database.

[Key]
    [StringLength(20)]
    public string pvid { get; set; }

Logically, it looks like it is already in the range. But for the sake of trying, i tried changing it to code below, and did add-migration. Then, in the Up() method, its empty. Does it mean that the modification is not recognized? Because when i tried running the project again, i encountered the same error as above.

[Key]
    [MaxLength(20), MinLength(12)]
    public string pvid { get; set; }

Then when i put breakpoints at my query, i see that:

InnerException: null

Message: The data reader is incompatible with the specified 'project.Models.tableName'. A member of the type 'columnName', does not have a corresponding column in the data reader with the same name.

Is there something wrong with my query?

            ChargeVM objDetails = new ChargeVM();
            var query3 = db.Database.SqlQuery<patient_visit>("SELECT MAX(CAST(SUBSTRING(pvid, 3, 10) AS int)) FROM patient_visit");

            if (query3 != null)
            {
                objDetails.pvid = query3.ToString();
                objDetails.pvid += 1;
                objDetails.pvid = "PV" + objDetails.pvid;
                }

            patient_visit pv = new patient_visit()
                    {
                        pvid = objDetails.pvid,
                        paid = paid
                    };

                    db.patient_visit.Add(pv);

I put a try catch for the db.SaveChanges() to catch any exceptions, and at line "throw raise", the exception error that i mentioned in the first paragraph pops up.

               try
                    {
                        db.SaveChanges();
                    }
                    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                    {
                        Exception raise = dbEx;
                        foreach (DbEntityValidationResult validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (DbValidationError validationError in validationErrors.ValidationErrors)
                            {
                                string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage);
                                // raise a new exception nesting
                                // the current instance as InnerException
                                raise = new InvalidOperationException(message, raise);
                            }
                        }
                        throw raise;
                    }

Actually i just switched to C# recently and i simply converted my codes from vb.net to C#. I am wondering if it was a problem with the syntax or anything because it was running smoothly previously with vb.net. Could anyone help point it out to me please? I'm new to this.