(Not new to SO, but new to asking questions, so pardon mistakes. Thanks.)
I used EF Power Tools to pre-generate views, but then I saw "return null;
" at the end of the following method of the generated class, and so I wanted to find out when (or if) GetView
actually returns null.
Here's the method:
/// <summary>
/// Gets a view corresponding to the specified extent.
/// </summary>
/// <param name="extent">The extent.</param>
/// <returns>The mapping view, or null if the extent is not associated with a mapping view.</returns>
public override DbMappingView GetView(EntitySetBase extent)
{
if (extent == null)
{
throw new ArgumentNullException("extent");
}
var extentName = extent.EntityContainer.Name + "." + extent.Name;
//...
if (extentName == "CodeFirstDatabase.Post")
{
return GetView22();
}
if (extentName == "CodeFirstDatabase.PostComment")
{
return GetView23();
}
//...
return null;
}
Here are my models:
public class Post
{
public Post()
{
comments = new HashSet<PostComment>();
}
//...
[InverseProperty("post")]
public virtual ICollection<PostComment> comments { get; set; }
//...
}
public class PostComment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[ForeignKey("comment")]
public long comment_id { get; set; }
public Post comment { get; set; }
[ForeignKey("post")]
public long post_id { get; set; }
[InverseProperty("comments")]
public Post post { get; set; }
}
My posts and comments have the same model (after all, they just store text). I use the PostComment
model to group the comments in a separate table so that queries on comments could be faster as they do not include actual posts.
Setting breakpoints on the "return null;
" line of the GetView
method gets me "CodeFirstDatabase.PostComment_comment
" for the extentName
, which I think refers to the comment foreign key on PostComment
model. Of course, it returned that null as EF Power Tool did not generate a view for it.
My questions are:
- Why didn't EF Power Tools generate a view for that relationship (or any other relationships)?
- Will returning null make EF to generate the view itself at runtime?
- If yes to 2, will this impact startup time for first query?
As a related fourth question, why is the name "CodeFirstDatabase
" used alongside the actual context name in the view generation?
I'm using EntityFramework 6.1.3 btw.