实体框架代码首先不创建多对多数据库表

时间:2016-03-17 10:06:21

标签: c# ef-code-first

我的代码第一个模型如下所示。应用程序创建表'VideoPosts',而不创建'ImagePosts'。 CoverImage和TileImage导航属性有问题,还是我遗漏了什么?我希望有一个图像表,就像视频表一样。

public class Post
    {

        public int PostID { get; set; }

        [ForeignKey("TileImageID")]
        public Image TileImage { get; set; }
        [Column("TileImageID")]
        public int? TileImageID { get; set; }
        [ForeignKey("TileImageID")]
        public Image CoverImage { get; set; }
        [Column("CoverImageID")]
        public int? CoverImageID { get; set; }

        public virtual ICollection<Image> Gallery { get; set; }
        public virtual ICollection<Video> VideoGallery { get; set; }
    }

    public class Video
    {
        public int VideoID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        [Required]
        public string Url { get; set; }
        public virtual ICollection<Post> Posts { get; set; }
    }

    public class Image
    {
        public int ImageID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        [Required]
        public string Path { get; set; }

        public virtual ICollection<Post> Posts { get; set; }
    }

Database created

4 个答案:

答案 0 :(得分:0)

除了我没有看到您的数据库有任何问题之外,您的一个外键注释是错误的。

    [ForeignKey("TileImageID")]
    public Image TileImage { get; set; }
    [Column("TileImageID")]
    public int? TileImageID { get; set; }
    [ForeignKey("CoverImageID")]
    public Image CoverImage { get; set; }
    [Column("CoverImageID")]
    public int? CoverImageID { get; set; }

答案 1 :(得分:0)

尝试在DataContext中输入:

    public DataContext() : base("DatabaseName")
    {
       // Database.SetInitializer<DataContext>(new DropCreateDatabaseAlways<DataContext>());
        Database.SetInitializer<DataContext>(new CreateDatabaseIfNotExists<DataContext>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }

删除物理数据库并尝试运行应用程序,然后应用程序将自动创建数据库,然后您将看到&#39; ImagePosts&#39;。在你的代码中,我没有看到任何错误

答案 2 :(得分:0)

您的代码没有显示这一点,但是从您获得的错误中我假设您重写OnModelCreating。这是IdentityDbContext配置实体框架映射的地方。这意味着如果要覆盖OnModelCreating,则需要调用基础,或者必须自己进行映射。

所以要么:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         base.OnModelCreating(modelBuilder);
    }

或者您进行映射:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
    modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}

答案 3 :(得分:0)

我设法使用自定义映射创建所需的表。 TileImage和CoverImage也在模型中,我认为模型没有其他问题。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

            modelBuilder.Entity<Post>()
                .HasMany<Image>(s => s.Gallery)
                .WithMany(c => c.Posts)
                .Map(cs =>
                {
                    cs.MapLeftKey("PostID");
                    cs.MapRightKey("ImageID");
                    cs.ToTable("PostImages");
                });
        }