实体框架 - 3个表彼此之间存在关系

时间:2016-11-13 07:41:37

标签: entity-framework ef-code-first entity code-first foreign-key-relationship

我有3张表如下:

ApplicationUser:

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

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

发表

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();

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

  

ALTER TABLE语句与FOREIGN KEY约束冲突   “FK_dbo.Posts_dbo.Albums_AlbumId”。冲突发生在数据库中   “aspnet-Link-20161012104217”,表“dbo.Albums”,列'Id'。

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

1 个答案:

答案 0 :(得分:1)

在您的代码中,您将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();