C# 在 MVC 类上创建主键字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10236819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Primary Key field on MVC class
提问by Peter
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:
我是 MVC 和 C# 的新手。我只是偶然发现它并发现它很有趣。我遇到了一个不允许我继续的问题。这是我的代码:
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:
我在谷歌上搜索答案是添加 [Key]了,public int AccountTypeID { get; set; }所以它看起来像这样:
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
但直到现在还没有结果。注意:我正在使用 MVC 4
采纳答案by dknaack
Description
描述
Entity Framework CodeFirst recognize the key, by default, by name.
Valid names are Idor <YourClassName>Id.
默认情况下,实体框架 CodeFirst 按名称识别键。有效名称为Id或<YourClassName>Id。
Your property should named Idor AccountTypesId
您的财产应命名Id或AccountTypesId
Another way is to use the ModelBuilderto specify the key.
另一种方法是使用ModelBuilder来指定密钥。
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
模式信息
回答by SaravananArumugam
Try using [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] property to indicate the key field.
尝试使用 [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 属性来指示键字段。
The regular field would go with EntityKeyPropert=false.
常规字段将使用 EntityKeyPropert=false。
回答by user3137281
Hi Peter it seens you are missing an "s"
嗨,彼得,它看到你缺少一个“s”
Your Account int should be:
您的 Account int 应该是:
public int AccountsTypeID { get; set; }
公共 int AccountsTypeID { 获取;放; }
Hope it can be solved; Fernando.
希望能解决;费尔南多。
回答by Shaharukh Pathan
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);
}
}

