6

We all know that Linq to SQL (and SQLmetal/exe) is really a "prototype" quality product (it's missing basic features like schema "refresh" and detection of null columns with default value).

Is there a way to automatically create my .dbml (similar to LINQ to SQL or SQLmetal) AND have it detect NOT NULL columns that have a default value?

Requirements: It needs to be just as "easy" to generate as linq-to-sql or sqlmetal.


Clarification of what I need it for:

I have many tables with DateCreated and DateModified fields as well as some bit and enum-like (int) columns with default values; all of which should be and are not null.

I will need to regenerate ("refresh") the .dbml after making changes to the database...so (re)setting the Auto-Generate (or Auto-Sync) property to True is not something I really care to be doing nearly every time I update the schema.

The following code (based on Handling Default Values With LINQ to SQL):

namespace Project.Data
{
    public partial class ProjectDataContext
    {
        partial void OnCreated()
        {
            if (this.DateCreated == null)
            {
                this.DateCreated = DateTime.UtcNow;
            }
        }
    }
}

doesn't compile (errors with ... does not contain a definition for 'DateCreated' ...). I really saw no reason it should compile...but I gave it a shot anyway. Maybe I just don't understand the context of the code from his example.

David Murdoch
  • 87,823
  • 39
  • 148
  • 191
  • possible duplicate of [How do I ensure Linq to Sql doesn't override or violate non-nullable DB default values?](http://stackoverflow.com/questions/201706/how-do-i-ensure-linq-to-sql-doesnt-override-or-violate-non-nullable-db-default-v) – bzlm Oct 05 '10 at 22:22
  • 4
    That post is 2 years old and hints at the idea that it may one day be supported. I'm looking to see if today is that day. :-) – David Murdoch Oct 05 '10 at 22:32
  • anyone know of progress on this yet? – David Murdoch Dec 27 '10 at 21:20
  • There is no progress on this and there will not be as linq2sql is not developing anymore. The code you are showing [is correct](http://stackoverflow.com/a/1154633/11683) and unfortunately seems to be the only feasible way. It does not work for you because you put the `OnCreated` in the definition of the context class, you need to put it in the entity class. – GSerg Nov 06 '16 at 19:11

1 Answers1

-1
this.DateCreated ?? DateTime.UtcNow

?? means if left side is null then use right side

Bonshington
  • 3,970
  • 2
  • 25
  • 20