Entity Framework - default values doesn't set in sql server table

前端 未结 4 1649
南方客
南方客 2020-11-30 12:32

SQL server 2005 database table has a column \'createdon\' for which default value set to getdate(). I am trying to add a record using entity framework. \'createdon\' column

4条回答
  •  孤街浪徒
    2020-11-30 13:36

    I've got around this issue by telling EF that the column is 'computed', and should therefore be left alone for inserts.

    If you look in the configuration for the generated entity

           namespace Data.Context
            {
                // Table
                internal partial class MyTableConfiguration : EntityTypeConfiguration
                {
                    public MyTableConfiguration(string schema = "dbo")
                    {    
                        ToTable(schema + ".MyTable");
                        HasKey(x => x.Id);
    
                        Property(x => x.ColumnName).HasColumnName("ColumnName").IsOptional().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
                        ....
    

提交回复
热议问题