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
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
thanks