Field is required when it is not .net Core entityframeworkcore

asp.net-core c# entity-framework-core

Question

I generated context and model by running nuget command line

Scaffold-DbContext "Server=(LocalDb)\MSSQLLocalDB;Initial Catalog=DummyDB;
Integrated Security=True;Connect Timeout=30;" 
Microsoft.EntityFrameworkCore.SqlServer -outputDir Context -Context DummyContext

When I tried to pass without optional field "address", it complaints "address" field is required before reaching to the post function. Any idea? Thanks

{
  "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "firstName": "string",
  "age": 0
}

My codes generate as following

DummyContext.cs

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace ProjectTemplate.Context
{
public partial class DummyContext : DbContext
{
    public DummyContext()
    {
    }

    public DummyContext(DbContextOptions<DummyContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Dummys> Dummys { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("Server=(LocalDb)\\MSSQLLocalDB;Initial Catalog=DummyDB;Integrated Security=True;Connect Timeout=30;");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Dummys>(entity =>
        {
            entity.Property(e => e.Id).ValueGeneratedNever();

            entity.Property(e => e.Address)
                .HasColumnName("address")
                .HasMaxLength(200);

            entity.Property(e => e.Age).HasColumnName("age");

            entity.Property(e => e.FirstName)
                .IsRequired()
                .HasColumnName("firstName")
                .HasMaxLength(50);
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

}

Dummys.cs

using System;
using System.Collections.Generic;

namespace ProjectTemplate.Context
{
public partial class Dummys
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public int? Age { get; set; }
    public string Address { get; set; }
}
}

a post function for createBook

    // POST: api/Dummy
    [HttpPost]
    public async Task<ActionResult<Dummys>> Post([FromBody] Dummys dummy)
    {
        dummy.Id = Guid.NewGuid();
        using (var context = new DummyContext())
        {
            context.Dummys.Add(dummy);
            context.SaveChanges();
        }
        return dummy;
    }
1
0
4/2/2020 9:22:26 PM

Popular Answer

Try to add to your address property .IsRequired(false) . This can work because the IsRequired property is always true and you can't do a string nullable as int, bool etc.

entity.Property(e => e.Address)
    .HasColumnName("address")
    .HasMaxLength(200)
    .IsRequired(false);

Try this.

0
4/2/2020 10:03:15 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow