0

I have this model:

public class Blog
{
    public int ID { get; set; }
    public string Title { get; set; }
}

public class Post
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Content { get; set; } 
    public int BlogID { get; set; }
    public Blog Blog { get; set; }
}

which has this configuration:

public class BlogMap : EntityTypeConfiguration<Blog>
{
    public BlogMap()
    {
        this.ToTable("Blogs", "dbo");
        this.HasKey(t => t.ID);
        this.Property(t => t.ID).HasColumnName("ID");
        this.Property(t => t.Title).HasColumnName("Title");
    }
}

public class PostMap : EntityTypeConfiguration<Post>
{
    public PostMap()
    {
        this.ToTable("Posts", "dbo");
        this.HasKey(t => t.ID);
        this.Property(t => t.ID).HasColumnName("ID");
        this.Property(t => t.Title).HasColumnName("Title");
        this.Property(t => t.Content).HasColumnName("Content");
        this.Property(t => t.BlogID).HasColumnName("BlogID");
        this.HasRequired(t => t.Blog)
            .WithRequiredDependent()
            .Map(???);
    }
}

How do I map this?

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59

1 Answers1

5

I'm guessing that if, like a normal blog, each blog can have many posts, then maybe you need to be configuring a one-to-many relationship:

this.HasRequired(t => t.Blog)
    .WithMany() // no arguments means no inverse property
    .HasForeignKey(t => t.BlogID);

As an aside, EF will probably be able to infer this relationship even if you don't configure it, but explicitly configuring it is perfectly fine.

jjj
  • 4,822
  • 1
  • 16
  • 39
  • This was some example that I came up with, not the real model. In this model, I don't really care what the blog has besides it's title and I don't want a navigation property on the `Blog` entity for every entity that references it. On my model, following your suggestion, I got something like __Invalid column name 'Blog_ID'__. – Paulo Morgado Jul 01 '15 at 20:21
  • It's actually doing the join right but selecting `Post.Blog_ID` for no apparent reason. – Paulo Morgado Jul 01 '15 at 20:29
  • @PauloMorgado: hmm, `Property(t => t.BlogID).HasColumnName("BlogID");` should have taken care of that... – jjj Jul 01 '15 at 20:35
  • The generated query was something like: `SELECT [Extent1].[ID], [Extent1].[Title], [Extent1].[Content], [Extent1].[BlogID], [Extent2].[ID], [Extent2].[Title], [Extent1].[Blog_ID] FROM [dbo].[Posts] AS [Extent1] INNER JOIN [dbo].[Blogs] as [Extent2] ON [Extent1].[BlogID] = [Extent2].[ID]` – Paulo Morgado Jul 01 '15 at 20:52
  • @PauloMorgado: do you have any other configuration for `Property(t => t.BlogID)`? does it work if you put the `Property(t => t.BlogID).HasColumnName("BlogID");` after the relationship configuration (that's more of a wild guess)? – jjj Jul 01 '15 at 21:13
  • @PauloMorgado: Ah, sorry, not sure why the naming isn't working properly. It looks like it should work, but maybe you could find [other](http://stackoverflow.com/questions/8573544/code-first-entity-framework-adds-an-underscore-to-a-primary-key-column-name) [related](http://stackoverflow.com/questions/25061378/how-to-expose-foreign-key-property-to-existing-entity-having-navigational-proper) answers or write another question. – jjj Jul 02 '15 at 05:52
  • IT WORKS!. The problem was that my example didn't had a `ICollection Posts` on the `Blog` class like my real code. As soon as I removed those navigation properties, it worked. – Paulo Morgado Jul 07 '15 at 15:06