The following code gives an exception
"Microsoft.Data.SqlClient.SqlException : Cannot insert the value NULL into column 'last_update_time', table 'asubio1-db.dbo.expt_table'; column does not allow nulls. INSERT fails."
Appears to be nothing wrong with the code below
and the project file is
"Microsoft.Data.SqlClient.SqlException : Cannot insert the value NULL into column 'last_update_time', table 'asubio1-db.dbo.expt_table'; column does not allow nulls. INSERT fails."
Appears to be nothing wrong with the code below
C#:
namespace expt;
using NUnit.Framework;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("expt_table")]
public class DbPageCompleted {
[Column("user_name")]
[MaxLength(50)]
[Required]
[Key]
public string? userName {get; set;}
[Column("last_update_time")]
[Required]
public DateTime? lastUpdateTime;
}
public class ExptContext : DbContext {
public DbSet<DbPageCompleted> pageCompletions { get; set; }
protected string connectionStr()
{
var connStr = Environment.GetEnvironmentVariable("SQLCONNSTR_DbConn");
if (connStr == null) throw new NoDbConnection();
return connStr;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string? connStr = connectionStr();
optionsBuilder.UseSqlServer(connStr);
}
public static ExptContext dbContext = new ExptContext();
}
[TestFixture]
public class ExptTests {
DbPageCompleted dbPageCompleted = new DbPageCompleted {
userName = "[EMAIL]test@localhost.com[/EMAIL]",
lastUpdateTime = DateTime.Now.ToUniversalTime()
};
[Test]
public void Expt() {
ExptContext dbContext = ExptContext.dbContext;
dbContext.pageCompletions.Add(dbPageCompleted);
dbContext.SaveChanges();
}
}
and the project file is
XML:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
</ItemGroup>
</Project>
Last edited by a moderator: