2
    public class Picture
        { 
            [Key]
            public int Id { get; set; }

            public int NewsId { get; set; }
            [ForeignKey("NewsId")]
            public virtual News News { get; set; }

            public int PostId { get; set; }
            [ForeignKey("PostId")]
            public virtual Post Post { get; set; }        
        }

    public class News
        {
            public int Id { get; set; }
            public virtual Picture Picture { get; set; }
        }

public class Post
    {
        public int Id { get; set; }
        public virtual Picture Picture { get; set; }
    }

Exception:

Unable to determine the principal end of an association between the types 'Project.BusinessObjects.Photo' and 'Project.BusinessObjects.Student'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

Whats wrong?

1 Answers1

0

I think you can solve the problem by creating new [key] data annotations above the other classes' keys. Like this:

public class Picture
{ 
    [Key]
    public int Id { get; set; }

    [ForeignKey("News")]
    public int NewsId { get; set; }
    public virtual News News { get; set; }

    [ForeignKey("Post")]
    public int PostId { get; set; }        
    public virtual Post Post { get; set; }        
}

public class News
{
    [Key]
    public int Id { get; set; }
    public virtual Picture Picture { get; set; }
}

public class Post
{
    [Key]
    public int Id { get; set; }
    public virtual Picture Picture { get; set; }
}