sql server - Code-first migration: foreign key with default value of empty Guid -


project details:

  • asp.net 4.5
  • mvc 5
  • ef 6

i doing code-first project, , in migration looking set default values lots of things. 1 thing involves 2 specific types of foreign keys lookup tables. since these lookup tables must always loaded default value selected, there 1 value in these lookup tables have guid.empty value (00000000-0000-0000-0000-000000000000) primary key, can identified default value.

i trying find way have foreign keys make use of these 2 lookup tables default empty guid if passed null value. this added insurance in case miss spot in code supposed replace null value empty guid, don’t. plus, in project in particular there scheduled weekly imports external database, , want able have imported data have default value in foreign key without having work. having foreign key have empty guid default value handy several different angles.

and yes, use guids as guids, not varchar(128) strings. these foreign keys non-nullable uniqueidentifier fields.

in terms of migration script, looking manually add default value field creation process, @ loss how manually insert empty guid default value uniqueidentifier field..

i have made post over @ dba.stackexchange handle ms sql server 2008 portion of question.

suggestions?

as per other post over @ dba.stackexchange, indications answer adding default guid.empty non-null uniqueidentifier field within migration script such:

[foreign key] = c.guid(nullable: false, defaultvalue: '00000000-0000-0000-0000-000000000000')); 

i have yet test out, if in error please comment on post.


Comments