How do I make my constructor protected in when using entity framework database first?
When I generate an Entity Data Model from my database, the auto generated class contains a public constructor,
//------------------------------------------------------------------------------
// <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 Domain.Models
{
using System;
using System.Collections.Generic;
public partial class MyClass
{
public MyClass()
{
what I would like is a protected constructor
public partial class MyClass
{
protected MyClass()
{
You can do this by modifying the t4 template that creates the model classes. Look for this part near the top of the file:
and replace the bottom line
public <#=code.Escape(entity)#>()
byThis change will create protected constructors for entity type. Lower in the file there is a similar construct for complex types, you may want to modify that one as well.
A word about "updates". If you update the model from the database, these changes will not be overwritten. However, if you update EF, a problem with modified t4 templates is that you have to do it again when you want to use the templates of the new version.