简体   繁体   English

Azure SQL 数据库抛出 Null 插入异常

[英]Azure SQL Database throws Null insertion exception

Programmatically seeding the database with the Azure App Service console throws the following error message:使用 Azure 应用服务控制台以编程方式为数据库播种会引发以下错误消息:

SqlException (0x80131904): Cannot insert the value NULL into column 'Id'

However, programmatically seeding the database locally is error free.但是,以编程方式在本地为数据库播种是没有错误的。 What could be the issue?可能是什么问题?

Failed Solutions:失败的解决方案:

  • Set the Id column as Nullable directly in database直接在数据库中将Id列设置为Nullable
  • Annotate the entity class with the [Keyless] attribute[Keyless]属性注释实体 class
  • Annotate the entity class's Id property with the [Key] attribute[Key]属性注解实体类的Id属性

Table Schema表模式

+-------------+---------------+-------------+
| Table Name: Cities                        |
+-------------+---------------+-------------+
| Column Name |   Data Type   | Allow Nulls | 
+-------------+---------------+-------------+
| Id (PK)     | int           |     NO      |
| CityCode    | int           |     NO      |
| Name        | nvarchar(MAX) |     YES     |
| State       | nvarchar(MAX) |     YES     |
| Country     | nvarchar(MAX) |     YES     | 
+-------------+---------------+-------------+

Entity Class:实体 Class:

public class City
{
  public int Id { get; set; }
  public int CityCode { get; set; }
  public string Name { get; set; }
  public string State { get; set; }
  public string Country { get; set; }
}

Seeding Code:播种代码:

public class DbInitializer : IDbInitializer
{
    private readonly IServiceScopeFactory _scopeFactory;
    private readonly IWebHostEnvironment _hostEnvironment;

    public DbInitializer(IServiceScopeFactory scopeFactory, IWebHostEnvironment environment)
    {
        _scopeFactory = scopeFactory;
        _hostEnvironment = environment;
    }

    public void Initialize()
    {
        using (var serviceScope = _scopeFactory.CreateScope())
        {
            using (var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>())
            {
                context.Database.EnsureCreatedAsync();
            }
        }
    }

    public async Task SeedData()
    {
        using (var serviceScope = _scopeFactory.CreateScope())
        {
            using (var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>())
            {
                if (!context.Cities.Any())
                {
                    string path = Path.Combine(_hostEnvironment.WebRootPath, "citylist.json");
                    using (StreamReader reader = new StreamReader(path))
                    {
                        string jsonData = reader.ReadToEnd();
                        List<City> cityList = JsonConvert.DeserializeObject<List<City>>(jsonData);
                        await context.Cities.AddRangeAsync(cityList);
                        await context.SaveChangesAsync();
                    }
                }
            }
        }
    }
}

Can you check if the source data is having a value as NULL instead of an actual NULL您能否检查源数据的值是否为 NULL 而不是实际的 NULL

Similar kind of case happened with me where the data was having NULL as string value and not the usual NULL, integer columns won't allow such strings类似的情况发生在我身上,数据有 NULL 作为字符串值,而不是通常的 NULL,integer 列不允许这样的字符串

Just to rule out this possibility, this check can be done if you can extract the source data in an excel and check for any such string value只是为了排除这种可能性,如果您可以提取 excel 中的源数据并检查任何此类字符串值,则可以完成此检查

The source of the problem was that the Id column was not set as the identity column, thus disabling auto increments on the column.问题的根源是Id列未设置为标识列,因此禁用了该列的自动递增。 And since I'm not providing an id value, the database attempted a Null insertion and threw the exception.由于我没有提供id值,数据库尝试插入 Null 并抛出异常。

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

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