如何在两个表之间建立两个关系?

时间:2014-03-14 08:41:10

标签: c# entity-framework asp.net-mvc-4 ef-code-first relational-database

我有一个Article,其中包含User表中的创建者和修饰符。这是模型:

public class Article
{
    public int ArticleId { get; set; }
    public string Name { get; set; }
    public int UserId { get; set; }
    public int ModifierId { get; set; }
    public virtual User User { get; set; }
    public virtual User Modifier { get; set; }
}

这是User模型:

public class User
{
    public int UserId { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    public virtual List<Article> Articles { get; set; }
}

我试过这个:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Entity<Article>().HasRequired(c => c.User).WithMany(u => u.Articles).HasForeignKey(c => c.UserId);
        modelBuilder.Entity<Article>().HasRequired(c => c.Modifier).WithMany(u => u.Articles).HasForeignKey(c => c.ModifierId);
        Database.SetInitializer<Context>(null);
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

但错误是:

  

指定的架构无效。错误:关系   'Portal.Infrastructure.Repository.Article_User'未加载,因为   “Portal.Infrastructure.Repository.User”类型不可用。

我使用的是ef6 code-first和.net4.5

2 个答案:

答案 0 :(得分:1)

试试这段代码:

public class Article
{
public int ArticleId { get; set; }
public string Name { get; set; }
public int UserId { get; set; }
public int ModifierId { get; set; }

[InverseProperty("UserArticles")]
[ForeignKey("UserId")]
public virtual User User { get; set; }

[InverseProperty("ModifierArticles")]
[ForeignKey("ModifierId")]
public virtual User Modifier { get; set; }
}


public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }

[InverseProperty("User")]
public virtual List<Article> UserArticles { get; set; }

[InverseProperty("Modifier")]
public virtual List<Article> ModifierArticles { get; set; }
}

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Article>().HasRequired(c => c.User).WithMany(u => u.UserArticles).HasForeignKey(c => c.UserId).WillCascadeOnDelete(false);
        modelBuilder.Entity<Article>().HasRequired(c => c.Modifier).WithMany(u => u.ModifierArticles).HasForeignKey(c => c.ModifierId).WillCascadeOnDelete(false);

      .
      .
      .
      .
    }

答案 1 :(得分:0)

我发现了这个问题。我改变了上下文:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Article>().HasRequired(c => c.User).WithMany().HasForeignKey(c => c.UserId);
    modelBuilder.Entity<Article>().HasRequired(c => c.Modifier).WithMany().HasForeignKey(c => c.ModifierId);
    Database.SetInitializer<Context>(null);
    base.OnModelCreating(modelBuilder);
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

我还改变了User这样的模型:

public class User
{
    public int UserId { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
}