default values on dbcontext

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
figueiredorj
Posts: 41
Joined: Fri 12 Apr 2013 19:05

default values on dbcontext

Post by figueiredorj » Fri 26 Oct 2018 17:43

Hi,

I have a dbcontext where I need default values from db.
Yes.. it seems that it is able to do so, but only on some dataatypes.

I thought that it could be related to dbcontext on which I was working with so I went for a clean dbcontext on latest entity developer release (6.3.606) just to ensure that is wasn't related about my 'cooked' dbcontext.

try to add this database table on a database:

Code: Select all

CREATE TABLE [dbo].[Book](
	[Id] [uniqueidentifier] NOT NULL,
	[Code] [nvarchar](21) NOT NULL,
	[Name] [nvarchar](50) NOT NULL,
	[Author] [nvarchar](50) NOT NULL,
	[PublishedCopies] [int] NULL,
	[BoughtCopies] [int] NULL,
	[Access_Inserted] [datetime] NOT NULL,
	[Access_Updated] [datetime] NULL,
	[Access_Active] [bit] NULL,
	[Access_Deleted] [datetime] NULL,
	[Description] [nvarchar](400) NULL,
	[Others] [nvarchar](50) NOT NULL,
	[Deleted] [bit] NOT NULL,
 CONSTRAINT [PK_Book] PRIMARY KEY CLUSTERED 
(
	[Name] ASC,
	[Author] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_Id]  DEFAULT (newid()) FOR [Id]
GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_Name]  DEFAULT (N'unknown') FOR [Name]
GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_PublishedCopies]  DEFAULT ((50)) FOR [PublishedCopies]
GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_BoughtCopies]  DEFAULT ((50)) FOR [BoughtCopies]
GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_Access_Inserted]  DEFAULT (getdate()) FOR [Access_Inserted]
GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_Access_Updated]  DEFAULT (getdate()) FOR [Access_Updated]
GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_Access_Active]  DEFAULT ((1)) FOR [Access_Active]
GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_Description]  DEFAULT (N'Nothing to Declare') FOR [Description]
GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_Others]  DEFAULT (N'Show me') FOR [Others]
GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_Deleted]  DEFAULT ((1)) FOR [Deleted]
GO
and then try fetch it on a clean dbcontext;

Code: Select all

    public partial class Book : ICloneable, INotifyPropertyChanging, INotifyPropertyChanged    {

        public Book()
        {
            OnCreated();
        }


        #region Properties
    
        /// <summary>
        /// There are no comments for Id in the schema.
        /// </summary>
        [System.ComponentModel.DataAnnotations.Required()]
        public virtual global::System.Guid Id
        {
            get
            {
                return _Id;
            }
            set
            {
                if (_Id != value)
                {
                    this.OnIdChanging(value);
                    OnPropertyChanging("Id");
                    _Id = value;
                    this.OnIdChanged();
                    OnPropertyChanged("Id");
                }
            }
        }
        private global::System.Guid _Id;

    
        /// <summary>
        /// There are no comments for Code in the schema.
        /// </summary>
        [System.ComponentModel.DataAnnotations.StringLength(21)]
        [System.ComponentModel.DataAnnotations.Required()]
        public virtual string Code
        {
            get
            {
                return _Code;
            }
            set
            {
                if (_Code != value)
                {
                    this.OnCodeChanging(value);
                    OnPropertyChanging("Code");
                    _Code = value;
                    this.OnCodeChanged();
                    OnPropertyChanged("Code");
                }
            }
        }
        private string _Code;

    
        /// <summary>
        /// There are no comments for Name in the schema.
        /// </summary>
        [System.ComponentModel.DataAnnotations.Key]
        [System.ComponentModel.DataAnnotations.StringLength(50)]
        [System.ComponentModel.DataAnnotations.Required()]
        public virtual string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                if (_Name != value)
                {
                    this.OnNameChanging(value);
                    OnPropertyChanging("Name");
                    _Name = value;
                    this.OnNameChanged();
                    OnPropertyChanged("Name");
                }
            }
        }
        private string _Name;

    
        /// <summary>
        /// There are no comments for Author in the schema.
        /// </summary>
        [System.ComponentModel.DataAnnotations.Key]
        [System.ComponentModel.DataAnnotations.StringLength(50)]
        [System.ComponentModel.DataAnnotations.Required()]
        public virtual string Author
        {
            get
            {
                return _Author;
            }
            set
            {
                if (_Author != value)
                {
                    this.OnAuthorChanging(value);
                    OnPropertyChanging("Author");
                    _Author = value;
                    this.OnAuthorChanged();
                    OnPropertyChanged("Author");
                }
            }
        }
        private string _Author;

    
        /// <summary>
        /// There are no comments for PublishedCopies in the schema.
        /// </summary>
        public virtual int? PublishedCopies
        {
            get
            {
                return _PublishedCopies;
            }
            set
            {
                if (_PublishedCopies != value)
                {
                    this.OnPublishedCopiesChanging(value);
                    OnPropertyChanging("PublishedCopies");
                    _PublishedCopies = value;
                    this.OnPublishedCopiesChanged();
                    OnPropertyChanged("PublishedCopies");
                }
            }
        }
        private int? _PublishedCopies = 50;

    
        /// <summary>
        /// There are no comments for BoughtCopies in the schema.
        /// </summary>
        public virtual int? BoughtCopies
        {
            get
            {
                return _BoughtCopies;
            }
            set
            {
                if (_BoughtCopies != value)
                {
                    this.OnBoughtCopiesChanging(value);
                    OnPropertyChanging("BoughtCopies");
                    _BoughtCopies = value;
                    this.OnBoughtCopiesChanged();
                    OnPropertyChanged("BoughtCopies");
                }
            }
        }
        private int? _BoughtCopies = 50;

    
        /// <summary>
        /// There are no comments for AccessInserted in the schema.
        /// </summary>
        [System.ComponentModel.DataAnnotations.Required()]
        public virtual global::System.DateTime AccessInserted
        {
            get
            {
                return _AccessInserted;
            }
            set
            {
                if (_AccessInserted != value)
                {
                    this.OnAccessInsertedChanging(value);
                    OnPropertyChanging("AccessInserted");
                    _AccessInserted = value;
                    this.OnAccessInsertedChanged();
                    OnPropertyChanged("AccessInserted");
                }
            }
        }
        private global::System.DateTime _AccessInserted = DateTime.Now;

    
        /// <summary>
        /// There are no comments for AccessUpdated in the schema.
        /// </summary>
        public virtual global::System.DateTime? AccessUpdated
        {
            get
            {
                return _AccessUpdated;
            }
            set
            {
                if (_AccessUpdated != value)
                {
                    this.OnAccessUpdatedChanging(value);
                    OnPropertyChanging("AccessUpdated");
                    _AccessUpdated = value;
                    this.OnAccessUpdatedChanged();
                    OnPropertyChanged("AccessUpdated");
                }
            }
        }
        private global::System.DateTime? _AccessUpdated = DateTime.Now;

    
        /// <summary>
        /// There are no comments for AccessActive in the schema.
        /// </summary>
        public virtual bool? AccessActive
        {
            get
            {
                return _AccessActive;
            }
            set
            {
                if (_AccessActive != value)
                {
                    this.OnAccessActiveChanging(value);
                    OnPropertyChanging("AccessActive");
                    _AccessActive = value;
                    this.OnAccessActiveChanged();
                    OnPropertyChanged("AccessActive");
                }
            }
        }
        private bool? _AccessActive;

    
        /// <summary>
        /// There are no comments for AccessDeleted in the schema.
        /// </summary>
        public virtual global::System.DateTime? AccessDeleted
        {
            get
            {
                return _AccessDeleted;
            }
            set
            {
                if (_AccessDeleted != value)
                {
                    this.OnAccessDeletedChanging(value);
                    OnPropertyChanging("AccessDeleted");
                    _AccessDeleted = value;
                    this.OnAccessDeletedChanged();
                    OnPropertyChanged("AccessDeleted");
                }
            }
        }
        private global::System.DateTime? _AccessDeleted;

    
        /// <summary>
        /// There are no comments for Description in the schema.
        /// </summary>
        [System.ComponentModel.DataAnnotations.StringLength(400)]
        public virtual string Description
        {
            get
            {
                return _Description;
            }
            set
            {
                if (_Description != value)
                {
                    this.OnDescriptionChanging(value);
                    OnPropertyChanging("Description");
                    _Description = value;
                    this.OnDescriptionChanged();
                    OnPropertyChanged("Description");
                }
            }
        }
        private string _Description;

    
        /// <summary>
        /// There are no comments for Others in the schema.
        /// </summary>
        [System.ComponentModel.DataAnnotations.StringLength(50)]
        [System.ComponentModel.DataAnnotations.Required()]
        public virtual string Others
        {
            get
            {
                return _Others;
            }
            set
            {
                if (_Others != value)
                {
                    this.OnOthersChanging(value);
                    OnPropertyChanging("Others");
                    _Others = value;
                    this.OnOthersChanged();
                    OnPropertyChanged("Others");
                }
            }
        }
        private string _Others;

    
        /// <summary>
        /// There are no comments for Deleted in the schema.
        /// </summary>
        [System.ComponentModel.DataAnnotations.Required()]
        public virtual bool Deleted
        {
            get
            {
                return _Deleted;
            }
            set
            {
                if (_Deleted != value)
                {
                    this.OnDeletedChanging(value);
                    OnPropertyChanging("Deleted");
                    _Deleted = value;
                    this.OnDeletedChanged();
                    OnPropertyChanged("Deleted");
                }
            }
        }
        private bool _Deleted;


        #endregion
    
        #region Extensibility Method Definitions
        partial void OnCreated();
        partial void OnIdChanging(global::System.Guid value);
        partial void OnIdChanged();
        partial void OnCodeChanging(string value);
        partial void OnCodeChanged();
        partial void OnNameChanging(string value);
        partial void OnNameChanged();
        partial void OnAuthorChanging(string value);
        partial void OnAuthorChanged();
        partial void OnPublishedCopiesChanging(int? value);
        partial void OnPublishedCopiesChanged();
        partial void OnBoughtCopiesChanging(int? value);
        partial void OnBoughtCopiesChanged();
        partial void OnAccessInsertedChanging(global::System.DateTime value);
        partial void OnAccessInsertedChanged();
        partial void OnAccessUpdatedChanging(global::System.DateTime? value);
        partial void OnAccessUpdatedChanged();
        partial void OnAccessActiveChanging(bool? value);
        partial void OnAccessActiveChanged();
        partial void OnAccessDeletedChanging(global::System.DateTime? value);
        partial void OnAccessDeletedChanged();
        partial void OnDescriptionChanging(string value);
        partial void OnDescriptionChanged();
        partial void OnOthersChanging(string value);
        partial void OnOthersChanged();
        partial void OnDeletedChanging(bool value);
        partial void OnDeletedChanged();
        #endregion
    
        #region ICloneable Members

        public virtual object Clone()
        {
            Book obj = new Book();
            obj.Id = Id;
            obj.Code = Code;
            obj.Name = Name;
            obj.Author = Author;
            obj.PublishedCopies = PublishedCopies;
            obj.BoughtCopies = BoughtCopies;
            obj.AccessInserted = AccessInserted;
            obj.AccessUpdated = AccessUpdated;
            obj.AccessActive = AccessActive;
            obj.AccessDeleted = AccessDeleted;
            obj.Description = Description;
            obj.Others = Others;
            obj.Deleted = Deleted;
            return obj;
        }

        #endregion
    
        #region Equals and GetHashCode methods

        public override bool Equals(object obj)
        {
          Book toCompare = obj as Book;
          if (toCompare == null)
          {
            return false;
          }

          if (!Object.Equals(this.Name, toCompare.Name))
            return false;
          if (!Object.Equals(this.Author, toCompare.Author))
            return false;
          
          return true;
        }

        public override int GetHashCode()
        {
          int hashCode = 13;
          hashCode = (hashCode * 7) + Name.GetHashCode();
          hashCode = (hashCode * 7) + Author.GetHashCode();
          return hashCode;
        }

        #endregion
    
        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        protected void OnPropertyChanging(string propertyName) {

          if (PropertyChanging != null)
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }

        #endregion
    
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName) {

          if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
  • uniqueidentifier misses default value;
  • you will see that defaults Datetime are fulfilled, int default values are fulfilled;
  • also int default values are fulfilled;
  • no default value on bit;
  • no default value on nvarchar/string
I really need to have default values for this datatypes. help please

thanks

Pinturiccio
Devart Team
Posts: 2420
Joined: Wed 02 Nov 2011 09:44

Re: default values on dbcontext

Post by Pinturiccio » Tue 30 Oct 2018 10:42

We have reproduced the issue. We will investigate it and post here about the results as soon as possible.

figueiredorj
Posts: 41
Joined: Fri 12 Apr 2013 19:05

Re: default values on dbcontext

Post by figueiredorj » Mon 05 Nov 2018 10:26

Hi,
got any news on this matter?

thanks

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: default values on dbcontext

Post by Shalex » Wed 07 Nov 2018 19:17

* The code generation of a default value for the String property mapped to the column with a default value, that contains a unicode literal with N-prefix, is supported in EF and EF Core.
* The code generation of DateTime.Now for the DateTime property mapped to the column with the default value SYSDATETIME() is supported in EF and EF Core.
* The code generation of DateTime.UtcNow for the DateTime property mapped to the column with the default value GETUTCDATE() or SYSUTCDATETIME() is supported in EF and EF Core.
* The code generation of DateTimeOffset.Now for the DateTimeOffset property mapped to the column with the default value SYSDATETIMEOFFSET() or GETDATE() is supported in EF and EF Core
* The code generation of DateTimeOffset.UtcNow for the DateTimeOffset property mapped to the column with the default value GETUTCDATE() or SYSUTCDATETIME() is supported in EF and EF Core.
* The code generation of a default value for the Guid property mapped to the column with the default value NEWID() и NEWSEQUENTIALID() is supported in EF1/EF4/EF5/EF6.
* The code generation of a default value for the Boolean property mapped to the column with the numeric default value is supported in EF1/EF4/EF5/EF6.

We will notify you when the new build of Entity Developer is available for download.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: default values on dbcontext

Post by Shalex » Thu 29 Nov 2018 14:58

New build of Entity Developer 6.3.631 is available for download: viewtopic.php?f=32&t=38114.

Post Reply