【问题标题】:Entity Framework - 3 tables have relationships with each other实体框架 - 3 个表之间有关系
【发布时间】:2016-11-13 07:41:36
【问题描述】:

我有3张表如下:

应用程序用户:

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”。

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

【问题讨论】:

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


    【解决方案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();
    

    【讨论】:

    • 这不是问题。模型构建器将始终覆盖 FK 类型。问题是违反了 FK 约束,所以有些帖子没有专辑(或不存在专辑)。
    • 它成功了,非常感谢,我以为“WithRequired”意味着每个专辑都必须有博客,结果恰恰相反
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多