Question

I have an Article that have a creator and a modifier from User table. Here is the model:

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; }
}

And here is the User model:

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; }
}

And I have tried this:

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

But the Error is:

Schema specified is not valid. Errors: The relationship 'Portal.Infrastructure.Repository.Article_User' was not loaded because the type 'Portal.Infrastructure.Repository.User' is not available.

I am using ef6 code-first and .net4.5

Was it helpful?

Solution

try this code :

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

      .
      .
      .
      .
    }

OTHER TIPS

I found the issue. I changed the Context as below:

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

And also I changed the User model like this:

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; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top