1
votes

I have the following SQL to create a table:

    public const string dropCreate =
        SET ANSI_NULLS ON
        SET QUOTED_IDENTIFIER ON
        IF EXISTS (SELECT name FROM sys.objects WHERE name = 'Application' AND type = 'U')
            DROP TABLE [dbo].[Application]
        CREATE TABLE [dbo].[Application] (
            [ApplicationId] INT IDENTITY (1, 1) NOT NULL,
            [Name] NVARCHAR (50) Not NULL,
            [RowVersion] [varbinary](max) NULL,
            [ModifiedDate] [datetime] NOT NULL,
            CONSTRAINT [PK_dbo.Application] PRIMARY KEY CLUSTERED ([ApplicationId] ASC)
        )";

Here's my class:

public partial class Application
{
    public Application()
    {
        this.TestAccounts = new List<TestAccount>();
    }

    public int ApplicationId { get; set; }
    public string Name { get; set; }
    public byte[] RowVersion { get; set; }
    public System.DateTime ModifiedDate { get; set; }
    public virtual ICollection<TestAccount> TestAccounts { get; set; }
}

Mapping:

    public ApplicationMap()
    {
        // Primary Key
        this.HasKey(t => t.ApplicationId);

        // Properties
        this.Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(35);

        this.Property(t => t.RowVersion)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(8)
            .IsRowVersion();

        // Table & Column Mappings
        this.ToTable("Application");
        this.Property(t => t.ApplicationId).HasColumnName("ApplicationId");
        this.Property(t => t.Name).HasColumnName("Name");
        this.Property(t => t.RowVersion).HasColumnName("RowVersion");
        this.Property(t => t.ModifiedDate).HasColumnName("ModifiedDate");
    }

I do an insert as follows:

       new TestAccount 
       { 
           Application = app ,
           Name = applicationName, 
           ModifiedDate = DateTime.Now 
       };

It's giving me the following exception

InnerException: System.Data.Entity.Infrastructure.DbUpdateException HResult=-2146233087 Message=A null store-generated value was returned for a non-nullable member 'RowVersion' of type 'Relational.Mappings.Contexts.Application'. Source=EntityFramework

Can someone give me some advice on this. As far as I know I have set everything up correctly.

2

2 Answers

0
votes

looks like your mapping:

 this.Property(t => t.RowVersion)
        **.IsRequired()**
        .IsFixedLength()
        .HasMaxLength(8)
        .IsRowVersion();

is different to your table:

[RowVersion] [varbinary](max) NULL,

They need to be consistent.

are you trying to set up a row version column as per http://msdn.microsoft.com/en-us/library/ms182776.aspx?

0
votes

Change the RowVersion To TimeStamp