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?