简体   繁体   中英

Caught “Specified cast is not valid.” error on receiving data in C# ASP.net MVC?

I'm new to Entity Framework. I was trying to get my data from my local database through this basic line of code, I wanted to store all of the objects in the "Object" row into a list.

But it seems like it doesn't work, whatever I try. I'm running SQL server, ASP.NET MVC. My code is something like these:

[HttpGet]
public List<Object> Function1()
{
    List<Object> result = new List<Object>();

    using (DatabaseContext db = new DatabaseContext())
    {
        result = db.Object.ToList();
        // ...
        return result;
    }
}

It always ended up with "Specified cast is not valid." error: 在此输入图像描述

This is where the error was caught:

Line 137: result = db.Object.ToList();

This is my model class, I added some functions though, but I haven't changed any default properties that Entity set up for me :

public partial class Object
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
        public int Like { get; set; }
        public int View { get; set; }
        public byte Level
        {
            get { return Level; }
            set
            {
                if (value < 1 || value > 3)
                {
                    Level = 1;
                    throw new Exception("Level must be in 1 to 3. By default, it becomes 1");
                }
                else
                {
                    Level = value;
                }
            }
        }
        public string Introduction { get; set; }
        public string VideoUrl { get; set; }
        public string Tag { get; set; }
        public string Steps { get; set; }

        public DateTime Date { get; set; }

        public Object(string name, byte level, string introduction = null)
        {
            this.Name = name;
            this.Level = level;
            this.Introduction = introduction;
        }
    }

Is it oke to add functions and fix the properties like that ??

This is my table design in sql : pic

You have used public byte Level auto-property with a custom setter method.

This should be accompanied with a private backing variable. Something like

private byte _level
public byte Level
{
    get { return _level; }
    set
    {
        if (value < 1 || value > 3)
        {
            _level = 1;
            throw new Exception("Level must be in 1 to 3. By default, it becomes 1");
        }
        else
        {
            _level = value;
        }
    }
}

You need to case the Object into a specific Model object something like this

[HttpGet]
public List<Object> Function1()
{
    List<Object> result = new List<Object>();
    using (DatabaseContext db = new DatabaseContext())
    {
        //result = db.Object;
        result = (from d in db.Object.ToList()
                  select new Object{
                      prop1 = d.prop1,
                      prop2 = d.prop2,
                  .......
            }).ToList();
    // ...
    return result;
}

}

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