Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC 5,在实体框架中插入不起作用_C#_Asp.net_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# ASP.NET MVC 5,在实体框架中插入不起作用

C# ASP.NET MVC 5,在实体框架中插入不起作用,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我的工作是为学校管理系统制作一个网站。我有学生和管理员可以登录系统。学生可以注册。我已经使用实体框架模板为ASP.NET MVC 5创建了控制器。它创建了帐户模型、控制器和创建、删除、详细信息、编辑、索引视图。帐户字段为: public int AccountID { get; set; } public Nullable<int> TypeID { get; set; } public string Password { get; set; } public string Firs

我的工作是为学校管理系统制作一个网站。我有学生和管理员可以登录系统。学生可以注册。我已经使用实体框架模板为ASP.NET MVC 5创建了控制器。它创建了帐户模型、控制器和创建、删除、详细信息、编辑、索引视图。帐户字段为:

public int AccountID { get; set; }
public Nullable<int> TypeID { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ICNumber { get; set; }
public string Telephone { get; set; }
public string email { get; set; }
public string Adress { get; set; }`
会计模型

尝试添加[Key]属性,并确保数据库设置为自动递增字段

[Key]
public int AccountID { get; set; }
控制器

您还可以从[BindInclude=]中删除AccountID,因为这不是由post设置的,而是在服务器上管理的。这不应该导致错误,但这是一种很好的做法

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register([Bind(Include = "TypeID,Password,FirstName,LastName,ICNumber,Telephone,email,Adress")] Account account)
{
    // stuff
}
SQL Server


在SQL Server上,对于标识规范,AccountID应设置为Yes,而Is Identity应设置为Yes。Identity Increment通常为1,Identity seed通常为1,但这不会导致插入出现任何问题。

尝试将数据属性[Key]置于帐户模型中AccountID属性的上方。了解您是先使用代码还是先使用数据库也会很有帮助。另一方面,不确定您的开发程度,但ASP.NET MVC 5具有标识,这是从用户帐户和身份验证开始的好方法。为您做了很多事情,您不必担心身份验证的所有细微差别,如密码的盐析和散列。@David Lee Database首先尝试添加[Key]属性,并确保您的数据库设置为自动递增字段。您也可以从[BindInclude=]中删除AccountID,因为这不是由post设置的,而是在服务器上管理的。@Davidle我知道这一点,但在将其连接到我自己的数据库时遇到问题,因此我决定进行自己的验证,除注册外,一切正常。谢谢!但还有一个问题,在我对模型做了一些更改后,我发现所有模型都被覆盖了。。。有什么方法可以阻止它吗?不客气,我在评论中发表了关于模型覆盖的问题。我不确定我是否能帮你。
[Key]
public int AccountID { get; set; }
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register([Bind(Include = "TypeID,Password,FirstName,LastName,ICNumber,Telephone,email,Adress")] Account account)
{
    // stuff
}