0

I am trying to insert data into a SQL Server table that has a lot of not null constraints:

CREATE TABLE [dbo].[Customer]
(
    [CustomerId] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [varchar](255) NOT NULL,
    [LastName] [varchar](255) NOT NULL,
    [AddressLine] [varchar](255) NOT NULL,
    [City] [varchar](55) NOT NULL,
    [StateCode] [varchar](3) NOT NULL,
    [ZipCode] [varchar](10) NOT NULL,

    CONSTRAINT [PK_Customer]  
        PRIMARY KEY CLUSTERED ([CustomerId] ASC)
 )

EF code:

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

modelBuilder.Entity<Customer>(entity =>
{
    entity.Property(e => e.FirstName)
        .HasMaxLength(255)
        .IsRequired()
        .IsUnicode(false);

    entity.Property(e => e.LastName)
            .HasMaxLength(255)
            .IsRequired()
            .IsUnicode(false);

    entity.Property(e => e.AddressLine)
            .HasMaxLength(255)
            .IsRequired()
            .IsUnicode(false);

    entity.Property(e => e.City)
            .HasMaxLength(55)
            .IsRequired()
            .IsUnicode(false);

    entity.Property(e => e.StateCode)
            .HasMaxLength(3)
            .IsRequired()
            .IsUnicode(false);

    entity.Property(e => e.ZipCode)
            .HasMaxLength(10)
            .IsRequired()
            .IsUnicode(false);

  });

When attempting to add data into table, code is missing columns, so it fails to insert into database. Did not know about this, and did not receive 'NOT NULL' errors, as I would see in SQL database. How would I report SQL Server errors back into C# ASP.NET MVC application? ([Required] attribute will work, but I want to view SQL Server errors in C#)

var source = new Customer();

source.FirstName = "Joe";
source.LastName = "Smith";  // missing Address, City, State, Zip, etc
_context.Customer.Add(source);

Error displayed in SQL Server:

Cannot insert the value NULL into column 'Last', table 'dbo.Customer'; column does not allow nulls. INSERT fails.

How would I get these errors in the C# ASP.NET MVC program?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Have you tried running EF migrations? If you have missing columns is because they haven't been inserted into the DB yet – Jose A Feb 18 '19 at 02:56
  • I checked my dbcontext file and database in sql server, plus seeding data properly with all columns, they are in sync –  Feb 18 '19 at 03:00
  • try...catch... If SQL throws this message, you would be able to get it in your exception. – monstertjie_za Feb 18 '19 at 05:50

2 Answers2

1

Add [Required] attribute to your property.

[Required]
public string FirstName{ get; set; }

then, if you try to save the Customer without assigning a value to the FirstName property then EF will throw the System.Data.Entity.Validation.DbEntityValidationException exception

try
{
   _context.SaveChanges();
}
catch (DbUpdateException e)
{
}
cdev
  • 5,043
  • 2
  • 33
  • 32
  • I know this will work, I guess general question is, how do I receive SQL server errors, and report them back to C# program? –  Feb 18 '19 at 03:14
  • You can't catch SqlException with entity framework. You can only capture mentioned exception in this url.https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext.savechanges?redirectedfrom=MSDN&view=entity-framework-6.2.0#overloads – cdev Feb 18 '19 at 03:36
  • Thanks, All I need is the try catch statement, I have, I have entity.Property(e => e.AddressLine) .HasMaxLength(255) .IsRequired(), is required in my context file, –  Feb 18 '19 at 06:13
  • this may be another good question, I have,thanks, accepted this answer https://stackoverflow.com/questions/54739423/should-automapper-be-used-to-map-from-viewmodel-back-into-model –  Feb 18 '19 at 06:14
0

If your field in the DB is not Nullable type(NOT NULL) then you should add [Required] attribute to make sure entity framework will check for null before making request to SQL Server

If the field is primary key then you should use [Key] attribute instead

Take a look at this tutorial here.

Dung Le
  • 104
  • 3