gpt4 book ai didi

asp.net-mvc-3 - Entity Framework 多对多

转载 作者:行者123 更新时间:2023-12-02 05:04:56 25 4
gpt4 key购买 nike

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

}

我搜索这个问题的原因我没有找到,我想了解为什么 Entity Framework 将用户添加到项目列表而不是将项目添加到用户列表

最佳答案

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

但是您可能没有使用User 加载Projects 列表。如果你打电话...

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

关于asp.net-mvc-3 - Entity Framework 多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12989699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com