C# 访问SQL数据库的MVC 5项目问题
我已经创建了一个带有登录和验证的简单项目。然而,我不知道为什么我不能用gmail注册这个程序。在我把我的gmail帐户注册的过程中,我会遇到这个错误C# 访问SQL数据库的MVC 5项目问题,c#,sql-server,database,entity-framework-6,C#,Sql Server,Database,Entity Framework 6,我已经创建了一个带有登录和验证的简单项目。然而,我不知道为什么我不能用gmail注册这个程序。在我把我的gmail帐户注册的过程中,我会遇到这个错误 System.Data.Entity.Infrastructure.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.' 在测试这个项目期间,我使用了hotmail帐户yao0529@live.
System.Data.Entity.Infrastructure.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.'
在测试这个项目期间,我使用了hotmail帐户yao0529@live.com和gmail帐户yuky0529@gmail.com登记。hotmail帐户注册成功,但gmail无法注册。除电子邮件外,这两种注册信息完全相同
下面的代码是普通数据类
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Food_Founder.Models
{
[MetadataType(typeof(UserMetaData))]
public partial class User
{
public string ConfirmPassword { get; set; }
}
public class UserMetaData
{
[Display(Name = "First Name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "This field is required")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "This field is required")]
public string LastName { get; set; }
[Display(Name = "Email ID")]
[Required(AllowEmptyStrings = false, ErrorMessage = "This field is required")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Display(Name = "Date Of Birth")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public Nullable<System.DateTime> DateOfBirth { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "This field is required")]
[DataType(DataType.Password)]
[MinLength(8, ErrorMessage = "Minimum 8 characters or numbers are required")]
public string Password { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "This field is required")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "The password is not match")]
public string ConfirmPassword { get; set; }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Food_Founder.Models
{
using System;
using System.Collections.Generic;
public partial class User
{
public int UserID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public Nullable<System.DateTime> DateOfBirth { get; set; }
public string Password { get; set; }
public bool IsEmailVerified { get; set; }
public System.Guid ActivationCode { get; set; }
}
}
这里我遇到的新问题是,当我设置数据库主键的自动增量时,它显示了这个错误
SQL70001 this statement is not recognized in this context
此错误显示为集合下有一条红线。另外,我找不到任何用于将BuildAction设置为None的数据库脚本
任何地方,如果有什么我想放的请告诉我你。。谢谢。要检查内部异常吗?它可能会说什么缺失或不正确。@Siavash但在哪里检查呢?我没有为此编写任何异常代码。@Siavash另外,此错误SqlException:违反主键约束“PK\uuu User\uuu 1788CCACEC6305F0”中有一个详细信息。无法在对象“dbo.User”中插入重复的密钥。重复的键值为(0)。声明已经终止。
那是你的问题。不要复制您的主键。SQL通常应该自动生成主键值(通过自动增量字段)..NET代码通常不应该指定键值。您没有显示实际尝试插入数据的代码,但我猜您可能在不应该插入的地方指定了一个ID。也许您应该显示该代码,因为这就是错误发生的地方…您不需要创建函数,不。事实上,这与您应该做的相反-当有多人同时使用您的系统时,很容易创建重复。通过将ID字段设置为自动递增的标识字段,让数据库自动处理它。您可以轻松地通过谷歌搜索如何设置它,在SSMS中配置它需要2秒钟(请参阅)。这应该是大多数表格的标准做法。数据库设计课程/教程也会向您介绍这个概念。
CREATE TABLE [dbo].[User] (
[UserID] INT IDENTITY (1, 1) NOT NULL PRIMARY KEY,
[FirstName] VARCHAR (50) NOT NULL,
[LastName] VARCHAR (50) NOT NULL,
[Email] VARCHAR (256) NOT NULL,
[DateOfBirth] DATE NULL,
[Password] NVARCHAR (MAX) NOT NULL,
[IsEmailVerified] BIT NOT NULL,
[ActivationCode] UNIQUEIDENTIFIER NOT NULL,
PRIMARY KEY CLUSTERED ([UserID] ASC)
);
GO
SET IDENTITY_INSERT [dbo].[User] ON
SQL70001 this statement is not recognized in this context