Search code examples
linq-to-sqlsql-server-2008dynamic-data

Using database default values with Linq to SQL codewise


I am using Dynamic Data with linq to SQL and SQL Server 2008.

I have a GUID column that gets his value from the default value with newguid(). When I set IsDbGenerated to true in the designer it works like a charm.

But when I renew table this property is set back to false again. So I added it to the metadata. For some reason it's not being pickup, "00000000-0000-0000-0000-000000000000" is being inserted in database. The displayname and readonly change are being pick up.

What am I missing?

[MetadataType(typeof(CMS_Data_HistoryMetadata))] public partial class CMS_Data_History {

}

[TableName("Content")]
public class CMS_Data_HistoryMetadata
{
    [DisplayName("Pagina Title")]
    public object pageTitleBar { get; set; }


    [ReadOnly(true)]
    [DisplayName("Versie")]
    public object version_date { get; set; }

    [ColumnAttribute(IsDbGenerated = true)]       
    public object entity_id;

}

Solution

  • I solved the problem by extending the partial insert en update class and check there if the guid is filled

    partial void InsertCMS_Data_History(CMS_Data_History instance) {

            if(instance.entity_id == Guid.Empty)
            {
                instance.entity_id = Guid.NewGuid();
            }
            this.ExecuteDynamicInsert(instance);
    
        }
    
        partial void UpdateCMS_Data_History(CMS_Data_History
    

    instance) { if (instance.version_date == DateTime.MinValue) { instance.version_date = DateTime.Now; } this.ExecuteDynamicUpdate(instance); }