C# 向实体添加新构造函数
我使用的是EF4.3。这是我的第一个项目,所以我一直在学习 在某些情况下,我需要为实体实现额外的构造函数。我创建了一个额外的分部类来实现这一点,所以实体用户将有一个关联的类User2 我注意到EF总体上不创建构造函数,但也有这样的例子:C# 向实体添加新构造函数,c#,.net,entity-framework,partial-classes,C#,.net,Entity Framework,Partial Classes,我使用的是EF4.3。这是我的第一个项目,所以我一直在学习 在某些情况下,我需要为实体实现额外的构造函数。我创建了一个额外的分部类来实现这一点,所以实体用户将有一个关联的类User2 我注意到EF总体上不创建构造函数,但也有这样的例子: public partial class User { public User() { this.BookmarkedDeals = new HashSet<BookmarkedDeal>(); thi
public partial class User
{
public User()
{
this.BookmarkedDeals = new HashSet<BookmarkedDeal>();
this.BookmarkedStores = new HashSet<BookmarkedStore>();
}
public System.Guid UserId { get; set; }
public int UserStatusId { get; set; }
public int UserRoleId { get; set; }
public System.DateTime CreatedOn { get; set; }
public System.DateTime LastVisitedOn { get; set; }
public virtual ICollection<BookmarkedDeal> BookmarkedDeals { get; set; }
public virtual ICollection<BookmarkedStore> BookmarkedStores { get; set; }
public virtual Subscriber Subscriber { get; set; }
}
公共部分类用户
{
公共用户()
{
this.BookmarkedDeals=新HashSet();
this.BookmarkedStores=new HashSet();
}
public System.Guid用户标识{get;set;}
public int UserStatusId{get;set;}
public int UserRoleId{get;set;}
public System.DateTime CreatedOn{get;set;}
public System.DateTime LastVisitedOn{get;set;}
公共虚拟ICollection BookmarkedDeals{get;set;}
公共虚拟ICollection BookmarkedStores{get;set;}
公共虚拟订户订户{get;set;}
}
这让我有点担心,因为通过设计器添加导航属性非常容易,而基本构造函数中的代码却丢失了
public User()
{
this.BookmarkedDeals = new HashSet<BookmarkedDeal>();
this.BookmarkedStores = new HashSet<BookmarkedStore>();
}
公共用户()
{
this.BookmarkedDeals=新HashSet();
this.BookmarkedStores=new HashSet();
}
我的问题是,我是否需要从其他构造函数中调用基构造函数,我是否应该在所有情况下使用代码(:this User())来调用我的基构造函数作为保护 EF(数据库优先或模型优先)创建那些默认构造函数
public User()
{
this.BookmarkedDeals = new HashSet<BookmarkedDeal>();
this.BookmarkedStores = new HashSet<BookmarkedStore>();
}
如果您添加一个新的构造函数,我将调用默认构造函数,以使集合的实例化在所有构造函数之间保持一致。但我认为它必须是this
,而不是base
,因为两个构造函数都在同一个类中,而不是在继承层次结构中:
public User(SomeType SomeParameter) : this()
{
//Do somthing with SomeParameter...
}
public User(SomeType SomeParameter) : this()
{
//Do somthing with SomeParameter...
}