How to set default value for POCO in EF CF?

In Entity Framework 4, only code (CTP3), how do you set the default value for a property in the POCO EntityConfiguration class?

public class Person { public int Id { get; set; } public string Name { get; set; } public DateTime CreatedOn { get; set; } } public class PersonConfiguration : EntityConfiguration<Person> { public PersonConfiguration() { Property(p => p.Id).IsIdentity(); Property(p => p.Name).HasMaxLength(100); //set default value for CreatedOn ? } } 
+43
default-value entity-framework entity-framework-4 ef-code-first
Jun 29 2018-10-06T00:
source share
7 answers

With the release of Entity Framework 4.3, you can do this through Migrations.

EF 4.3 Primary passage of the first code

Thus, using your example, it will be something like this:

 public partial class AddPersonClass : DbMigration { public override void Up() { CreateTable( "People", c => new { Id = c.Int(nullable: false, identity: true), Name = c.String(maxLength: 100), CreatedOn = c.DateTime(nullable: false, defaultValue: DateTime.UtcNow) }) .PrimaryKey(t => t.Id); } public overide void Down() { // Commands for when Migration is brought down } } 
+24
Feb 24 2018-12-12T00:
source share

You can set default values ​​through the constructor for the class. Here is the class that I have in my current project using MVC3 and Entity Framework 4.1:

 namespace MyProj.Models { using System; using System.Collections.Generic; public partial class Task { public Task() { this.HoursEstimated = 0; this.HoursUsed = 0; this.Status = "To Start"; } public int ID { get; set; } public string Description { get; set; } public int AssignedUserID { get; set; } public int JobID { get; set; } public Nullable<decimal> HoursEstimated { get; set; } public Nullable<decimal> HoursUsed { get; set; } public Nullable<System.DateTime> DateStart { get; set; } public Nullable<System.DateTime> DateDue { get; set; } public string Status { get; set; } public virtual Job Job { get; set; } public virtual User AssignedUser { get; set; } } } 
+15
Jun 01 2018-11-12T00:
source share

I know that this topic has been going on for some time, and I went into this or that problem. Until now, I could not find a solution for me that keeps everything together in one place, so the code is still readable.

When creating a user, I want some fields to be set by the object itself through private setters , for example. The GUID and Creation Date, not the "pollution" of the constructor.

My user class:

 public class User { public static User Create(Action<User> init) { var user = new User(); user.Guid = Guid.NewGuid(); user.Since = DateTime.Now; init(user); return user; } public int UserID { get; set; } public virtual ICollection<Role> Roles { get; set; } public virtual ICollection<Widget> Widgets { get; set; } [StringLength(50), Required] public string Name { get; set; } [EmailAddress, Required] public string Email { get; set; } [StringLength(255), Required] public string Password { get; set; } [StringLength(16), Required] public string Salt { get; set; } public DateTime Since { get; private set; } public Guid Guid { get; private set; } } 

Call Code:

 context.Users.Add(User.Create(c=> { c.Name = "Name"; c.Email = "some@one.com"; c.Salt = salt; c.Password = "mypass"; c.Roles = new List<Role> { adminRole, userRole }; })); 
+7
May 30 '12 at 20:20
source share
+4
Jun 29 2018-10-06T00:
source share

I'm not sure which version of EF will become available, but from version 5 you are sure to handle it. Just set the default value for GetDate () in the SQL column, either in TSQL or in the constructor, then configure your EF classes as follows:

 public class Person { public int Id { get; set; } public string Name { get; set; } public DateTime CreatedOn { get; set; } } public class PersonConfiguration : EntityConfiguration<Person> { public PersonConfiguration() { Property(p => p.Id).IsIdentity(); Property(p => p.Name).HasMaxLength(100); this.Property(p => p.CreatedOn ).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed); } } 

Note that you do not need the database column to be null, or the POCO property.

+3
Apr 02 '13 at
source share

In case SQL Server needs to provide a default date, as you do, the important part is defaultValueSql: "GETDATE ()", which will set the default value for sql server, and not just evaluate the script time (i.e. DateTime. UtcNow)

  public partial class AddPersonClass : DbMigration { public override void Up() { CreateTable( "People", c => new { Id = c.Int(nullable: false, identity: true), Name = c.String(maxLength: 100), CreatedOn = c.DateTime(nullable: false, defaultValueSql: "GETDATE()") }) .PrimaryKey(t => t.Id); } public overide void Down() { // Commands for when Migration is brought down } } 
+3
May 22 '14 at
source share

There is no other way than using the MIGRATION database , and to enable migration you must set your property as follows

 [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime CreatedOn { get; set; } 

And after that, once your database is created, you must follow these steps to enable the migration. Go to the program manager console and run the following commands with one command

  • PM> Enable-Migrations
  • PM> Add-Migration AlterMyTable Here AlterMyTable can be any name. this will create a file in the new Migrations folder in your solution currentTimestamp_AlterMyTable.cs -Copy the next Up method from currentTimestamp_AlterMyTable.cs
  • AlterColumn("Person", "CreatedOn", n => n.DateTime(nullable: false, defaultValueSql: "SYSUTCDATETIME()")); And after that, run the last command in the program manager console
  • PM> Update-Database And now, if you see the Person table in your database, and then go to the CreatedOn column, it should have a default value of SYSUTCDATETIME() , that is, now if you try to insert some data into this table, your database will automatically update this CreatedOn column for you.
0
May 24 '17 at 11:09 a.m.
source share



All Articles