I do have defaults set to the fields in SQL Server. Now I’m using LINQ to access/append/modify and delete records in my table but I wondering why do I need to assign values to the fields that I have a default value upon insertion. Consider this line of code.
SQL
CREATE TABLE SampTable
(
sysid int not null primary key identity(1,1),
values1 bigint not null default(0), --> consider my default is predefined
values2 datetime not null default(getdate())
)
C# using LINQ
SampleTable entity = new SampleTable();
entity.values1 = 1;
entity.values2 = DateTime.Now //I do have default set on sql why do i need to do this
//Do Something
The ORM mapper you use (perhaps DBML) does not copy the default constraint from the database. Not a bad thing– what would happen if you changed the default in the database? Some old code might still have a copy from the old default in it’s ORM! That could get fairly confusing.
I’d suggest to start writing SQL yourself. SQL is not half as hard as writing or customizing an ORM. Either a stored procedure or an
insertstatement in C# code would work.