简体   繁体   English

尝试在数据库表中插入新条目时出现LINQ to SQL异常

[英]LINQ to SQL exception when attempting to insert a new entry in a database table

I'm trying to insert a user in my database via LINK to SQL and I'm getting an exception: 我试图通过LINK to SQL在我的数据库中插入一个用户,我得到一个例外:

Cannot insert the value NULL into column 'UserID', table 'mydatabase.developer.Users'; 无法将值NULL插入列'UserID',表'mydatabase.developer.Users'; column does not allow nulls. 列不允许空值。 INSERT fails. INSERT失败。 The statement has been terminated. 该语句已终止。

I've marked the UserID as the primary key in my Users table and I was expecting that the SQL Server will automatically generate a PK when I try to insert a new user in the table. 我已将UserID标记为Users表中的主键,并且当我尝试在表中插入新用户时,我预计SQL Server将自动生成PK。

I pretty much copied and pasted the example from Pro ASP.NET MVC Framework Chapter 4 Section on "Setting Up LINQ to SQL." 我几乎复制并粘贴了Pro ASP.NET MVC框架第4章“设置LINQ to SQL”中的示例。 Everything is in order... my database has a Users table with a UserID (PK) column and a Name column (both non-nullable), below is the class corresponding to the database table: 一切都井然有序......我的数据库有一个Users表,其中包含UserID(PK)列和Name列(都是非可空的),下面是与数据库表对应的类:

public class User
{
    [DisplayName("User ID")]
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    internal int UserID { get; set; }

    [DisplayName("Name")]
    [Column]
    public string Name{ get; set; }
}

I also have a repository class which allows me to modify the database: 我还有一个存储库类,允许我修改数据库:

public class UsersRepository : IUsersRepository
{
    private DataContext database;
    private Table<User> usersTable;

    public UsersRepository(string connectionString)
    {
        database = new DataContext(connectionString);
        usersTable = database.GetTable<User>();
    }

    public void AddUser(User user)
    {
        usersTable.InsertOnSubmit(user);
        try
        {
            database.SubmitChanges();
        }
        catch (Exception e)
        {
            var msg = e.Message;
        }
    }
    //...
}

IUsersRepository ur = new UsersRepository(connectionString);
ur.AddUser(new User{Name = "Joe"});

I've also tried setting the UserID to -1, 0 and 1, but I get the same exception! 我也尝试将UserID设置为-1,0和1,但我得到了相同的异常!

ur.AddUser(new User{UserID = -1, Name = "Joe"});
ur.AddUser(new User{UserID = 0, Name = "Joe"});
ur.AddUser(new User{UserID = 1, Name = "Joe"});

Does anybody know what may be happening here? 有谁知道这里可能会发生什么? I'm really baffled by this: what could cause the SQL server not generating the primary key? 我真的很困惑:什么可能导致SQL服务器不生成主键? Is there any way I can inspect the insert statement and verify what is actually being sent to the server? 有什么方法可以检查插入语句并验证实际发送到服务器的是什么?

Is your primary key an integer? 你的主键是整数吗? You need to set the identity specification: 您需要设置标识规范: 在此输入图像描述

If you are using something like a uniqueidentifier, you need to generate on the client with 如果您使用的是uniqueidentifier之类的东西,则需要在客户端上生成

User user = new User();

user.Id = Guid.NewGuid();

Check the properties of the table and make sure UserId allow nulls is set to false; 检查表的属性,并确保UserId允许空值设置为false; make sure auto-increment is set to yes 确保自动增量设置为是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM