Multiple Relationships with Entity Framework

I am using EntityFramework 4.1.

I have the following model:

public class User 
{
   [Key]
   public int Id { get; set; }
   public string Username { get; set; } 
   public string Password { get; set; }

   public virtual User Creator { get; set; }
   public virtual User LastEditor { get; set; }
}

When I try to create my database, I got this error:

It is not possible to determine the main end of the relationship between the User and User types. The primary goal of this association should be explicitly configured using either a free API or annotation data.

But I also have this class:

public class Course 
{
   [Key]
   public int Id { get; set; }
   public string Name { get; set; } 

   public virtual User Creator { get; set; }
   public virtual User LastEditor { get; set; }
}

And it works fine, I have these foreign keys:

Creator_Id
LastEditor_Id

Do I have something to add to my User model to make it work?

+5
source share
1 answer

EF - - - User.Creator User.LastEditor, User, . , Course. Course.Creator Course.LastEditor EF " " User. User, Fluent API:

modelBuilder.Entity<User>()
    .HasOptional(u => u.Creator)     // or HasRequired
    .WithMany();

modelBuilder.Entity<User>()
    .HasOptional(u => u.LastEditor)  // or HasRequired
    .WithMany();
+7

All Articles