【问题标题】:Mapping table name with class in mvc在 mvc 中将表名与类映射
【发布时间】:2014-06-03 07:36:13
【问题描述】:

在我的数据库中,我有一个名为tblEmp 的表。现在我在我的模型中创建了一个类Employee

using System.ComponentModel.DataAnnotations;

namespace MvcDemo.Models
{
    [Table("tblEmp")]
    public class Employee
    {
        public int Eid { get; set; }
        public string   Name    { get; set; }
        public string Gender { get; set; }
        public int Did { get; set; }
    }
}

我用过using System.ComponentModel.DataAnnotations;,也加了System.Data.Entity,但还是显示:

类型或命名空间名称“找不到表”

【问题讨论】:

  • 您使用的是哪个版本的实体框架?在 EF 5 中,它已更改为使用 System.ComponentModel.DataAnnotations.Schema;此外,我假设您在项目中正确安装了 nuget 的实体框架。对吗?
  • 是的。我是从 nuget manager 安装的。
  • 请试试我上面的建议?另外,您还没有回答您使用的是哪个版本的 EF?
  • 更改了您的标签。这个问题与MVC无关,与EF 无关。

标签: .net asp.net-mvc entity-framework ef-code-first


【解决方案1】:

你在这里给了错误的表名[Table("tblUser")]它应该是[Table("tblEmp")]。确保表中指定的列名和数据类型(此处为 tblEmp)与类(此处为 Employee)的属性名称和数据类型相匹配。

【讨论】:

    【解决方案2】:

    我想你忘了把 .Schema 放在你的使用

    如果您的table name 已经与您的Entity Class 的名称相同,则无需对其进行映射,否则使用表映射

    using System.ComponentModel.DataAnnotations.Schema;
    
    namespace MvcDemo.Models
    {
        [Table("tblEmployees")] //Exact name of the table should be put in here
        public class Employee
        {
            public int Eid { get; set; }
            public string   Name    { get; set; }
            public string Gender { get; set; }
            public int Did { get; set; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 2019-12-19
      • 1970-01-01
      相关资源
      最近更新 更多