为什么在添加实体之前必须初始化导航集合? [英] Why do I have to initialize navigation collection before adding Entities?
问题描述
CTP5,SQL CE 4.
为什么在添加实体之前必须初始化我的navigaion集合?
file.Tags = new Collection< Tag> ;();
$
file.Tags.Add(db.Tags.Find(1));
CTP5, SQL CE 4.
Why do I have to initialize my navigaion collection before adding an entity?
file.Tags = new Collection<Tag>();
file.Tags.Add(db.Tags.Find(1));
这是我的模型和初始化种子方法。 />
公共类PhotoTaggerDB:DbContext
{
public DbSet< UserFile> UserFiles {get;组; }
public DbSet< Tag>标签{get;组; }
public DbSet< WebAccount> WebAccounts {get;组; }
}
Here is my model and initialization seed methods.
public class PhotoTaggerDB : DbContext
{
public DbSet<UserFile> UserFiles { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<WebAccount> WebAccounts { get; set; }
}
公共类用户文件
{
public int ID {get;组; }
public string FileName {get;组; }
public string Path {get;组; }
public DateTime CreatedDate {get;组; }
public class UserFile
{
public int ID { get; set; }
public string FileName { get; set; }
public string Path { get; set; }
public DateTime CreatedDate { get; set; }
public virtual ICollection< Tag>标签{get;组; }
}
public virtual ICollection<Tag> Tags { get; set; }
}
公共类标签
{
public int ID {get;组; }
public string TagName {get;组; }
public class Tag
{
public int ID { get; set; }
public string TagName { get; set; }
public virtual ICollection< UserFile>档案{get;组; }
}
public virtual ICollection<UserFile> Files { get; set; }
}
公共类DBInitializer:DropCreateDatabaseAlways< PhotoTaggerDB>
{
$
protected override void Seed (PhotoTaggerDB上下文)
{
PhotoTaggerDB db = new PhotoTaggerDB();
public class DBInitializer : DropCreateDatabaseAlways<PhotoTaggerDB>
{
protected override void Seed(PhotoTaggerDB context)
{
PhotoTaggerDB db = new PhotoTaggerDB();
标记标记;
tag = new Tag();
tag.TagName =" Doors"; $
db.Tags.Add(tag );
Tag tag;
tag = new Tag();
tag.TagName = "Doors";
db.Tags.Add(tag);
tag = new Tag();
tag.TagName =" Fireplaces";
  ; db.Tags.Add(tag);
tag = new Tag();
tag.TagName = "Fireplaces";
db.Tags.Add(tag);
tag = new Tag();
tag.TagName =" Kitchens";
db.Tags.Add(tag);
tag = new Tag();
tag.TagName = "Kitchens";
db.Tags.Add(tag);
tag = new Tag();
tag。 TagName =" Sinks"; $
db.Tags.Add(tag);
tag = new Tag();
tag.TagName = "Sinks";
db.Tags.Add(tag);
db.SaveChanges();
db.SaveChanges();
UserFile文件;
UserFile file;
file = new UserFile();
file.FileName =" abc.pdf" ;;
file.Path = @" c:\ tps \tre \" ;;
file .CreatedDate = DateTime.Now;
file.Tags = new Collection< Tag>();
file.Tags.Add(db.Tags.Find (1));
$
db.UserFiles.Add(file);
file = new UserFile();
file.FileName = "abc.pdf";
file.Path = @"c:\tps\tre\";
file.CreatedDate = DateTime.Now;
file.Tags = new Collection<Tag>();
file.Tags.Add(db.Tags.Find(1));
db.UserFiles.Add(file);
file = new UserFile();
file.FileName =" bger.pdf" ;;
file.Path = @" c:\ tps \" ;;
file.CreatedDate = DateTime.Now;
file.Tags = new Collection< Tag>();
file.Tags.Add(db。 Tags.Find(1));
$
file.Tags.Add(db.Tags.Find(2));
db.UserFiles.Add(文件);
file = new UserFile();
file.FileName = "bger.pdf";
file.Path = @"c:\tps\";
file.CreatedDate = DateTime.Now;
file.Tags = new Collection<Tag>();
file.Tags.Add(db.Tags.Find(1));
file.Tags.Add(db.Tags.Find(2));
db.UserFiles.Add(file);
file = new UserFile();
file.FileName =" erty.pdf";
file.Path = @" c:\eeww \" ;;
file.CreatedDate = DateTime.Now;
file .Tags = new Collection< Tag>();
file.Tags.Add(db.Tags.Find(1));
file.Tags .Add(db.Tags.Find(2));
file.Tags.Add(db.Tags.Find(3));
db 。用户Files.Add(file);
file = new UserFile();
file.FileName = "erty.pdf";
file.Path = @"c:\eeww\";
file.CreatedDate = DateTime.Now;
file.Tags = new Collection<Tag>();
file.Tags.Add(db.Tags.Find(1));
file.Tags.Add(db.Tags.Find(2));
file.Tags.Add(db.Tags.Find(3));
db.UserFiles.Add(file);
file = new UserFile();
file.FileName =" ytre.pdf";
file.Path = @" c:\eeww \" ;;
$
file.CreatedDate = DateTime.Now;
file.Tags = new Collection< Tag>();
file.Tags.Add(db.Tags.Find(1));
  ; file.Tags.Add(db.Tags.Find(2));
file.Tags.Add(db.Tags.Find(3));
file.Tags.Add(db.Tags.Find(4));
$
db.UserFiles.Add(file);
file = new UserFile();
file.FileName = "ytre.pdf";
file.Path = @"c:\eeww\";
file.CreatedDate = DateTime.Now;
file.Tags = new Collection<Tag>();
file.Tags.Add(db.Tags.Find(1));
file.Tags.Add(db.Tags.Find(2));
file.Tags.Add(db.Tags.Find(3));
file.Tags.Add(db.Tags.Find(4));
db.UserFiles.Add(file);
db.SaveChanges();
}
db.SaveChanges();
}
推荐答案
嗨Terrence,
Hi Terrence,
当你创建一个新的类实例时,这就是CLR的工作原理。如果你希望它始终被初始化,那么你可以在构造函数中添加一行:
When you create a new instance of a class this is just how the CLR works. If you want it to be always initialized then you can add a line to your constructor:
public class UserFile
{
public UserFile()
{
this.Tags = new List<Tag>();
}
...
}
~Rowan
这篇关于为什么在添加实体之前必须初始化导航集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!