简体   繁体   English

在C#ASP.net MVC中接收数据时出现“指定的强制转换无效。”错误?

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

I'm new to Entity Framework. 我是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. 我正在运行SQL服务器,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(); 第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 : 这是我的模型类,但我添加了一些函数,但我没有更改Entity为我设置的任何默认属性:

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 这是我在sql: pic中的表设计

You have used public byte Level auto-property with a custom setter method. 您已使用自定义setter方法的public byte Level自动属性。

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 您需要将Object置于特定的Model对象中

[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;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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