Entity Framework - 3个表之间有关系

Entity Framework - 3 tables have relationships with each other

我有3个表如下:

应用程序用户:

public class ApplicationUser : IdentityUser
{
    ..some basic properties..

    // navigation properties
    public virtual ICollection<Post> Posts { get; set; }
    public virtual ICollection<Album> Albums { get; set; }
}

Post:

public class Post
{
        public long Id { get; set; }    
        public string Content { get; set; }

        public int? AlbumId { get; set; }
        public string UserId { get; set; }

        public virtual ApplicationUser User { get; set; }
        public virtual Album Album { get; set; }
}

专辑:

public class Album
{
        public int Id { get; set; }
        public string Name { get; set; }
        public string UserId { get; set; }

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

最后 ApplicationDbContext:

modelBuilder.Entity<ApplicationUser>()
                .HasMany(a=>a.Posts)
                .WithRequired(a=>a.User)
                .HasForeignKey(a=>a.UserId)
                .WillCascadeOnDelete(false);
modelBuilder.Entity<Post>()
                .HasKey(p => p.Id);

modelBuilder.Entity<Album>()
                .HasKey(a => a.Id);

modelBuilder.Entity<ApplicationUser>()
                .HasMany(u=>u.Albums)
                .WithOptional()
                .HasForeignKey(a=>a.UserId)
                .WillCascadeOnDelete();

modelBuilder.Entity<Album>()
                .HasMany(a=>a.Posts)
                .WithRequired()
                .HasForeignKey(p=>p.AlbumId)
                .WillCascadeOnDelete();

当我运行迁移和更新数据库时,出现错误:

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Posts_dbo.Albums_AlbumId". The conflict occurred in database "aspnet-Link-20161012104217", table "dbo.Albums", column 'Id'.

谁能告诉我为什么他们会发生冲突?这对我来说似乎很合法。

在您的代码中,您将 AlbumId 设置为 nullable,但在配置中定义了 WithRequeired():

public class Post
{
    public long Id { get; set; }    
    public string Content { get; set; }

    public int? AlbumId { get; set; }       //<-- this one
    public string UserId { get; set; }

    public virtual ApplicationUser User { get; set; }
    public virtual Album Album { get; set; }
}

modelBuilder.Entity<Album>()
            .HasMany(a=>a.Posts)
            .WithRequired() //<-- this one
            .HasForeignKey(p=>p.AlbumId)
            .WillCascadeOnDelete();

如果 AlbumIdnullable 您应该更改配置:

//Ef by default conventions set the AlbumId as foreign key
modelBuilder.Entity<Album>()
            .HasMany(a=>a.Posts)
            .WithOptional(a=>a.Album);

如果 AlbumId 不是 nullable 则更改 属性:

public class Post
{
    public long Id { get; set; }    
    public string Content { get; set; }

    public int AlbumId { get; set; }     
    public string UserId { get; set; }

    public virtual ApplicationUser User { get; set; }
    public virtual Album Album { get; set; }
}

并使用以下配置:

//Ef by default conventions set the AlbumId as foreign key
modelBuilder.Entity<Album>()
            .HasMany(a=>a.Posts)
            .WithRequired(a=>a.Album)
            .WillCascadeOnDelete();