Setting the Default Value of a Datetime Property to Datetime.Now Inside the System.Componentmodel Default Value Attrbute

Setting the default value of a DateTime Property to DateTime.Now inside the System.ComponentModel Default Value Attrbute

You cannot do this with an attribute because they are just meta information generated at compile time. Just add code to the constructor to initialize the date if required, create a trigger and handle missing values in the database, or implement the getter in a way that it returns DateTime.Now if the backing field is not initialized.

public DateTime DateCreated
{
get
{
return this.dateCreated.HasValue
? this.dateCreated.Value
: DateTime.Now;
}

set { this.dateCreated = value; }
}

private DateTime? dateCreated = null;

Setting Default Value to a property on c#

The Year property of DateTime is a calculated value but you're trying to embed it into an attribute which is an immutable value stored in the binary. These two concepts aren't compatible.

What you want to do here is have a marker to note that the value isn't set yet and to return the current Year in that case. This is a good place to use a nullable

int? _selYear;
public int SelectedYear {
get { return _selYear ?? DateTime.Today.Year; }
set {
_selYear = value;
UpdateCalendar();
}
}

Create default values in Entityframe work 6.1

Well you need to set the default value in the POCO, you can set it in the property backing field or in the constructor as follow.

1. Constructor

public class Agent
{
public Agent()
{
CreatedDate = DateTime.Now;
}

public DateTime CreatedDate { get; set; }
}

2. Property Backing Field

public class Agent
{
private DateTime _createdDate;
public DateTime Date
{
get { return _createdDate == default(DateTime) ? DateTime.Now : _createdDate; }
set { _createdDate = value; }
}
}

How can I add default values to a property when saving using Code First EF4.1?

Override SaveChanges in your derived DbContext:

public override int SaveChanges()
{
foreach(var entry in ChangeTracker.Entries<EditableBase>())
{
var entity = entry.Entity;
if (entry.State == EntityState.Added)
{
entity.CreatedOn = ...;
entity.CreatedBy = ...;
}
else if (entry.State == EntityState.Modified)
{
entity.ModifiedOn = ...;
entity.ModifiedBy = ...;
}
}

return base.SaveChanges();
}

I'm only not sure if generic Entries will work directly with your base type becasue it is not actually mapped as base entity. There is also non generic version so you can rewrite it to more complex linq query or test each entry's entity type in loop.

How can I alter a model to set the SQL default date when using EF code migrations

You can accomplish that in the DbContext class:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Property(x => x.CreatedOn).HasDefaultValueSql("getdate()");
.....
});

edit: needed to have the entity property of the modelBuilder in there too:

modelBuilder.Entity<TestModel>().Property...



Related Topics



Leave a reply



Submit