DLinq and null values and defaults


Is there any way to specify how DLinq handles null values - for example:

[Table]
public class Thing {
string name;
[Column]
public string Name {
get { return name; }
set { name = value; }
}
}

CREATE TABLE Thing
(
Name NVARCHAR(120) NOT NULL CONSTRAINT DF_Thing_Name DEFAULT(N'No Name')
)

... results in an INSERT command that specifies NULL for [Name] when it would be helpful to indicate DEFAULT.

Using stored procedures will handle it, but something like

[Column(NullValueUpdate = NullValueUpdate.SetDefault)]

would be handy...

Thanks.



Answer this question

DLinq and null values and defaults

  • Alain Metrics

    Thanks.

    It would still be helpful to offer insert+refresh/update+refresh in one go for selected fields. AutoGen=true already generates 'insert... select [id]' commands. Presumably some other flag could cause other fields to be refreshed on each update/insert, thus supporting database-originated changes.


  • Seema

    I have verified that this is a bug. Refresh will currently not work with xml mapping - if you have an attribute mapping it will work fine. The details are that internally a new context is used to perform the requery, and this context is defaulting to an attribute mapping - it is not inheriting the mapping source of the original context as it should be.

  • Jas001

    Looking a bit further, it appears that the default DLinq behaviour is not to 'refresh' a domain object's 'columns' after inserts or updates. Permiting this behaviour would be useful when working with defaults defined in the DB.

    Maybe something like:

    [Column(RefreshOnUpdates = RefreshOnUpdates.Always)]

    ...could work.

    Thanks.

    PS. IMO, the existing UpdateCheck enum would be more intuitive if named ConcurrencyCheck (ditto the corresponding property on ColumnAttribute).


  • THizle7XU

    I'm also seeing an error with context.Refresh(t, RefreshMode.KeepChanges) - I get a NullReferenceException from...

    at System.Data.DLinq.CommonDataServices.Requery(Object item, Boolean clearCache)
    at System.Data.DLinq.DataContext.Refresh(IEnumerable items, RefreshMode mode)
    at System.Data.DLinq.DataContext.Refresh(Object item, RefreshMode mode)

    I've double checked that the instance that I am passing to DataContext.Refresh() is not null.

    Thanks.


  • ggo6

    You can perform a manual refresh by using the DataContext.Refresh method, which performs a requery:

    Thing t = ... // newly inserted Thing
    DataContext context = ... // your data context
    context.Refresh(t, RefreshMode.KeepChanges);



  • neo_assyrian

    Of course, context.Refresh() does not sort out the original INSERT statement and handling null values and corresponding defaults in the DB - being able to request INSERT statements that do not specify NULL for null objects where DB defaults are wanted would still be handy...

    Thanks.


  • epotter


    At present, the DLinq documentation states that "AutoGen members are synchronized immediately after the data row is inserted and are available after SubmitChanges() completes."

    When I add an object that looks like:

    <Table Name="Address">
    <Type Name="Address">
    <Column Name="Id" Member="Id" Storage="id" IsIdentity="true" IsAutoGen="true" />
    <Column Name="Line1" Member="Line1" Storage="line1" />
    <!-- ... -->
    <!-- This is supposed to mean "when the record was first saved" - it is enforced in a trigger -->
    <Column Name="CreatedUtc" Member="CreatedOnData" Storage="createdOn" IsAutoGen="true" />
    <Column Name="RecordVersion" Member="VersionData" Storage="version" IsVersion="true" />
    </Type>
    </Table>

    I get a command like:

    exec sp_executesql N'INSERT INTO [Address](Line1, {...}) VALUES(@p0, {...})
    SELECT [t0].[Id], CONVERT(Binary,[t0].[RecordVersion]) AS [value]
    FROM [Address] AS [t0]
    WHERE [t0].[Id] = (CONVERT(BigInt,@@IDENTITY))',{...}

    Where only the IsIdentity and IsVersion columns are refreshed and going to be "available after SubmitChanges() completes".

    Regards,

    Frank


  • DLinq and null values and defaults