Share via

.net-8 entityframework core on SQLServer - dbcontext.add seems to ignore values in non-key fields

Anonymous
2024-08-09T17:13:24+00:00

The program below gives the following 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.

Code


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")]     [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 = "******@localhost.com",         lastUpdateTime = DateTime.Now.ToUniversalTime()     };

    [Test]     public void Expt() {         ExptContext dbContext = ExptContext.dbContext;         dbContext.pageCompletions.Add(dbPageCompleted);         dbPageCompleted.lastUpdateTime = DateTime.Now.ToUniversalTime();         dbContext.SaveChanges();     } }

The project file is

<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>


Windows for home | Other | Apps

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. DaveM121 891.6K Reputation points Independent Advisor
    2024-08-09T17:49:01+00:00

    Hi, I am Dave, I will help you with this.

    I apologize, Community is just a consumer forum, due to the scope of your question can you please post this question to our sister forum on Microsoft Q&A (The Developers and Forum) in the C# section (linked below).

    Over there you will have access to a host of developers and C# programming experts and will get a knowledgeable and quick answer to this question.

    https://learn.microsoft.com/en-us/answers/tags/...

    Was this answer helpful?

    0 comments No comments