1

I'm trying to create a one-to-one relation between two tables, but as a result I have one-to-many. What is the problem with this code?

namespace EFCF_Demo.Models
{
    public class Post
    {
        [Key]
        public int ID { get; set; }
        public string Title { get; set; }
        public string MiniContent { get; set; }
        public string Author { get; set; }
        public DateTime PublishDate { get; set; }
        public int Rating { get; set; }
        public virtual Content MainContent { get; set; }   
    }

    public class Content
    {
        public int ID { get; set; }
        public virtual Post Post { get; set; }
        public string FullContent { get; set; }
    }

    public class PostEntities : DbContext
    {
        public DbSet<Post> Posts { get; set; }
        public DbSet<Content> Contents { get; set; }
    }
}
marapet
  • 54,856
  • 12
  • 170
  • 184
Ihor Bats
  • 169
  • 1
  • 12

3 Answers3

2

Don't you need PostId in the Content class, and ContentId in the Post class?

        public class Content
        {
            [Key]
            public int PostId { get; set; }
            public virtual Post Post { get; set; }
            public string FullContent { get; set; }
        }

what about this:) This should do it.

hazimdikenli
  • 5,709
  • 8
  • 37
  • 67
1

Problem was resolved by removing

public DbSet<Content> Contents { get; set; }

After that we don't need to use the Fluent API but I have some problems with saving.

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
Ihor Bats
  • 169
  • 1
  • 12