How can you change the parameterless constructor of an Entity Framework 6 database-first generated class?

ef-database-first entity-framework-6

Question

On previous versions of EF (4, 5) I had the following extension to my User class:

public partial class User {
    public User()
    {
        DateCreated = DateTime.Now;
    }
}

But on EF6, the code generation creates a class with an already defined default parameterless constructor, and as such I get the following compile-time error:

Type 'Core.Models.User' already defines a member called 'User' with the same parameter types

How can I ensure the initialization of the DateCreated value on construction with EF6 database-first? More generally: how can I create a custom parameterless constructor in EF6 *generated classes?

EDIT: Here's a reduced version of the autogenerated class for reference:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Core.Models
{
    using System;
    using System.Collections.Generic;

    public partial class User
    {
        public User()
        {
            this.Nodes = new HashSet<Node>();
            this.UserOpenIDs = new HashSet<UserOpenID>();
            this.Flags = new HashSet<Flag>();
            this.AwardedMedals = new HashSet<AwardedMedal>();
        }

        public int UserID { get; set; }
        public string Email { get; set; }
        public string DisplayName { get; set; }
        public System.DateTime DateCreated { get; set; }
        public string Location { get; set; }
        public string RealName { get; set; }

        public virtual ICollection<Node> Nodes { get; set; }
        public virtual ICollection<UserOpenID> UserOpenIDs { get; set; }
        public virtual ICollection<Flag> Flags { get; set; }
        public virtual ICollection<AwardedMedal> AwardedMedals { get; set; }
    }
}
1
3
11/13/2014 6:13:09 PM

Accepted Answer

You can't implement a 2nd parameterless constructor User(), because there is already one in the auto-generated class.

Hence, you have only 2 options to resolve this issue:

  1. Inherit from the original class User (see below).

  2. Modify the T4-template so the parameterless constructor is generated with the extra code DateCreated = DateTime.Now; you want to add. That will generate the extra code each time you refresh your data model from the database.

If you want to choose option 1, do it as shown below:

public class myUser: User 
{
    public myUser(): base()
    {
        DateCreated = DateTime.Now;
    }
}

N.B.: You will have to use type casts in order to support your class myUser.

1
12/4/2014 11:43:50 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow