Entity framework 4 使用实体框架4的实体和映射如何处理null(ICollection)?
我有一个倒下的实体:Entity framework 4 使用实体框架4的实体和映射如何处理null(ICollection)?,entity-framework-4,code-first,ef-code-first,Entity Framework 4,Code First,Ef Code First,我有一个倒下的实体: public class Post { public long PostId { get; private set; } public DateTime date { get; set; } [Required] public string Subject { get; set; } public User User { get; set; } public Categ
public class Post
{
public long PostId { get; private set; }
public DateTime date { get; set; }
[Required]
public string Subject { get; set; }
public User User { get; set; }
public Category Category { get; set; }
[Required]
public string Body { get; set; }
public virtual ICollection<Tag> Tags { get; private set; }
public Post()
{
Category = new Category();
}
public void AttachTag(string name, User user)
{
if (Tags.Count(x => x.Name == name) == 0)
Tags.Add(new Tag {
Name = name,
User = user
});
else
throw new Exception("Tag with specified name is already attached to this post.");
}
public Tag DeleteTag(string name)
{
Tag tag = Tags.Single(x => x.Name == name);
Tags.Remove(tag);
return tag;
}
public bool HasTags()
{
return (Tags != null || Tags.Count > 0);
}
公共类职位
{
公共长PostId{get;私有集;}
公共日期时间日期{get;set;}
[必需]
公共字符串主题{get;set;}
公共用户{get;set;}
公共类别{get;set;}
[必需]
公共字符串体{get;set;}
公共虚拟ICollection标记{get;private set;}
公职人员职位()
{
类别=新类别();
}
public void AttachTag(字符串名称、用户)
{
if(Tags.Count(x=>x.Name==Name)==0)
添加(新标签{
Name=Name,
用户=用户
});
其他的
抛出新异常(“具有指定名称的标记已附加到此帖子。”);
}
公共标记DeleteTag(字符串名称)
{
Tag Tag=Tags.Single(x=>x.Name==Name);
标签。移除(标签);
返回标签;
}
公共bool HasTags()
{
返回(Tags!=null | | Tags.Count>0);
}
问题在于虚拟ICollection标记{get;private set;}
当里面没有标记时,它实际上显示为null。我不能初始化它,因为它需要是虚拟的
如何处理实体中的空值?如何初始化标记以及在何处
谢谢。即使是虚拟的,也可以初始化(实际上必须初始化)。这是从POCO T4模板生成的代码:
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Csob.Arm.EntityGenerator", "1.0.0.0")]
public virtual ICollection<TransactionCodeGroup> TransactionCodeGroups
{
get
{
if (_transactionCodeGroups == null)
{
_transactionCodeGroups = new FixupCollection<TransactionCodeGroup>();
}
return _transactionCodeGroups;
}
set
{
_transactionCodeGroups = value;
}
}
private ICollection<TransactionCodeGroup> _transactionCodeGroups;
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(“Csob.Arm.EntityGenerator”,“1.0.0.0”)]
公共虚拟ICollection TransactionCodeGroup
{
得到
{
if(_transactionCodeGroups==null)
{
_transactionCodeGroups=new FixupCollection();
}
返回交易代码组;
}
设置
{
_transactionCodeGroups=值;
}
}
私有ICollection\u TransactionCodeGroup;
正如您看到的,集合在第一次调用getter时被初始化。您可以初始化(实际上您必须),即使它是虚拟的。这是从POCO T4模板生成的代码:
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Csob.Arm.EntityGenerator", "1.0.0.0")]
public virtual ICollection<TransactionCodeGroup> TransactionCodeGroups
{
get
{
if (_transactionCodeGroups == null)
{
_transactionCodeGroups = new FixupCollection<TransactionCodeGroup>();
}
return _transactionCodeGroups;
}
set
{
_transactionCodeGroups = value;
}
}
private ICollection<TransactionCodeGroup> _transactionCodeGroups;
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(“Csob.Arm.EntityGenerator”,“1.0.0.0”)]
公共虚拟ICollection TransactionCodeGroup
{
得到
{
if(_transactionCodeGroups==null)
{
_transactionCodeGroups=new FixupCollection();
}
返回交易代码组;
}
设置
{
_transactionCodeGroups=值;
}
}
私有ICollection\u TransactionCodeGroup;
正如您所见,集合在第一次调用getter时初始化