在MVC类上创建主键字段 [英] Creating Primary Key field on MVC class

查看:133
本文介绍了在MVC类上创建主键字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC和C#的新手。我只是偶然发现,发现它很有趣。我遇到一个不允许我继续进行的问题。
这是我的代码:

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;

命名空间MyHotel.Models
{
public class AccountTypes
{
public int AccountTypeID {get;组; }
public string AccountTypeName {get;组; }
}
}

此后创建了控制器和视图。 / p>

为此,我发现这个错误:



在模型生成期间检测到一个或多个验证错误:

  System.Data.Edm.EdmEntityType::EntityType'AccountTypes'没有定义键。定义此EntityType的键。 
System.Data.Edm.EdmEntitySet:EntityType:EntitySetAccountTypes基于没有键定义的类型AccountTypes。

我google答案是要添加 [Key] 超过 public int AccountTypeID {get;组; } 所以可能如下所示:

 命名空间MyHotel.Models 
{
public class AccountTypes
{
[Key]
public int AccountTypeID {get;组; }
public string AccountTypeName {get;组; }
}
}

但直到现在还没有结果。
注意:我正在使用MVC 4

解决方案

描述



实体框架CodeFirst默认情况下,按名称识别密钥。
有效的名称是 Id < YourClassName> Id



您的财产应命名为 Id AccountTypesId



另一种方法是使用 ModelBuilder 来指定密钥。



样本



  public class MyDbContext:DbContext 
{
public DbSet< AccountTypes> AccountTypes {get;组;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity< AccountTypes> .HasKey(x => x.AccountTypeID);
base.OnModelCreating(modelBuilder);
}
}



模式信息




I am new to MVC and C#. I just stumbled on it and found it interesting. I encountered an issue which will not allow me proceed. Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; 

namespace MyHotel.Models
{
    public class AccountTypes
    {
        public int AccountTypeID { get; set; }
        public string AccountTypeName { get; set; }
    }
}

I created the controler and the view thereafter.

And for this, I keep got this error:

One or more validation errors were detected during model generation:

System.Data.Edm.EdmEntityType: : EntityType 'AccountTypes' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet "AccountTypes" is based on type "AccountTypes" that has no keys defined.

I google that the answers were to add [Key] over the public int AccountTypeID { get; set; } so it could look like this:

namespace MyHotel.Models
{
    public class AccountTypes
    {
        [Key]
        public int AccountTypeID { get; set; }
        public string AccountTypeName { get; set; }
    }
}

But no result until now. Note: I am using MVC 4

解决方案

Description

Entity Framework CodeFirst recognize the key, by default, by name. Valid names are Id or <YourClassName>Id.

Your property should named Id or AccountTypesId

Another way is to use the ModelBuilder to specify the key.

Sample

public class MyDbContext : DbContext
{
    public DbSet<AccountTypes> AccountTypes { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AccountTypes>.HasKey(x => x.AccountTypeID);
        base.OnModelCreating(modelBuilder);
    }
}

Mode Information

这篇关于在MVC类上创建主键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆