3
public class Project
{


    public virtual int ID { get; set; }

    [Required]
    public virtual String Title { get; set; }


    public String Definition { get; set; }
    public DateTime StartDate { get; set; }


    [Required]
    public int CreaterID { get; set; }

    public virtual ICollection<Status> Status { get; set; }
    public virtual ICollection<Task> Tasks { get; set; }
    public virtual ICollection<User> Users { get; set; }

    public Project()
    {
        Users = new HashSet<User>();
    }


}


public class User
{
    public int ID { get; set; }


    [DisplayName("Kullanıcı Adı")]
    [Required]
    [MinLength(5, ErrorMessage = "Kullanıcı Adı En Az 5 Karakter Olmalıdır")]
    public string username { get; set; }

    [DataType(DataType.Password)]
    [DisplayName("Şifre")]        
    [Required]
    [MinLength(3,ErrorMessage="Şifre En Az  3 Karakter Olmalıdır")]
    public string password { get; set; }

    [Required]
    public String Name { get; set; }

    [Required]
    public String Surname { get; set; }

    public int? CreaterID { get; set; }
    public int level { get; set; }

    public ICollection<Task> Tasks { get; set; }
    public ICollection<Project> Projects { get; set; }

    public User()
    {
        Projects = new HashSet<Project>();
    }
}


public class TaskDB : DbContext
{
    public DbSet<Comment> Comments { get; set; }
    public DbSet<Project> Projects { get; set; }
    public DbSet<Situation> Situaitons { get; set; }
    public DbSet<Task> Tasks { get; set; }
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Project>().
          HasMany(c => c.Users).
          WithMany(p => p.Projects).
          Map(
           m =>
           {
               m.MapLeftKey("ProjectId");
               m.MapRightKey("UserId");
               m.ToTable("ProjectUser");
           });
    }

}

如果我添加项目,当前用户添加到项目用户列表但项目未添加当前用户的项目列表这是我的项目添加代码

[HttpPost]
    public ActionResult Create(Project proje,Status status)
    {

        proje.StartDate = DateTime.Now;
        proje.Status = new HashSet<Status>();
        var user = _db.Users.Single(r=> r.ID == UserRole.ID);
        proje.Users.Add(user);
        proje.Status.Add(status);
        user.Projects.Add(proje);


        if (ModelState.IsValid)
        {
            var projeler = _db.Projects;
            projeler.Add(proje);
            _db.SaveChanges();

            return RedirectToAction("Index");

        }


            return View(proje);

    }

我搜索这个问题的原因我没有找到,我想了解为什么实体框架将用户添加到项目列表但不将项目添加到用户列表

4

1 回答 1

0

您将新项目添加到数据库的代码看起来是正确的,并且很可能已存储了关系。

但可能你没有ProjectsUser. 如果你打电话...

var project = _db.Projects.Single(p => p.ID == 1);
var users = project.Users; // lazy loading because Users is virtual

...您将看到项目的用户,因为他们被延迟加载,因为该Project.Users属性被标记为virtual. 如果你对User...

var user = _db.Users.Single(u => u.ID == 1);
var projects = user.Projects; // no lazy loading because Projects is not virtual

...项目未加载,因为该User.Projects属性未标记为virtual.

也可以标记该属性virtual以启用User.Projects集合的延迟加载:

public virtual ICollection<Project> Projects { get; set; }

或者使用急切加载:

var user = _db.Users.Include(u => u.Projects).Single(u => u.ID == 1);
于 2012-10-20T15:48:41.740 回答