1

我正在尝试在两个表之间创建一对一的关系,但结果是我有一对多的关系。这段代码有什么问题?

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; }
    }
}
4

3 回答 3

2

您不需要 Content 类中的 PostId 和 Post 类中的 ContentId 吗?

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

这个呢:) 这应该可以。

于 2011-05-10T12:27:36.243 回答
1

问题已通过删除解决

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

之后我们不需要使用 Fluent API,但我在保存时遇到了一些问题。

于 2011-01-18T09:21:33.900 回答