我有两个实体类定义为
1)项目类
public class Project
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ProjectId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime CreatedDate { get; set; }
public ICollection<Person> Persons { get; set; }
}
2)人员类
public class Person
{
public Person()
{
Projects = new List<Project>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int PersonId { get; set; }
public string Name { get; set; }
public string EmailAddress { get; set; }
public string Designation { get; set; }
public string CellPhone { get; set; }
public ICollection<Project> Projects { get; set; }
}
在我的DataContext类中,配置定义为
public class DataContext: DbContext
{
public DataContext()
: base("name=DataContext")
{
}
public DbSet<Project> Projects { get; set; }
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>()
.HasMany<Person>(p => p.Persons)
.WithMany(p => p.Projects)
.Map(m =>
{
m.ToTable("ProjectPersons");
m.MapLeftKey("ProjectId");
m.MapRightKey("PersonId");
});
}
}
创建新项目后,项目表和Person表填充了它的记录,但我注意到ProjectPerson表是通过加入而不是用ProjectId和PersonId填充的。
有什么建议吗?
这是我创建新项目的HttpPost方法
[HttpPost]
public ActionResult CreateProject(Projects newProject, int page = 1)
{
_ctx.Projects.AddProject(newProject, ref _ctx);
_ctx.Person.AddPerson(newProject.Person, newProject, ref _ctx);
_ctx.SaveChanges();
var userProjects = _ctx.Projects.GetStaffUserProjects(providerUserKey, page, ref _ctx);
return PartialView("_Projects", userProjects);
}
这是我的扩展方法'AddProject'和'AddPerson'
1)AddProject
public static void AddProject(this IEnumerable<Project> projects, Projects newProject, ref DataContext ctx)
{
var project = new Project
{
Title = newProject.Title,
Duration = newProject.Duration,
Description = newProject.Description,
CreatedDate = DateTime.Now,
Status = true,
UserId = (int)Membership.GetUser().ProviderUserKey,
ProjectCategoryId = newProject.SelectedProjectCategoryId,
OrganizationId = newProject.SelectedOrganizationId
};
ctx.Projects.Add(project);
}
2)AddPerson
public static void AddPerson(this IEnumerable<Person> persons, Person newPerson, Projects newProject, ref DataContext ctx)
{
var person = new Person
{
Name = newPerson.Name,
EmailAddress = newPerson.EmailAddress,
CellPhone = newPerson.CellPhone,
Designation = newPerson.Designation
};
ctx.Person.Add(person);
}
}
答案 0 :(得分:1)
要创建人与项目之间的关系,您必须将已添加或附加到上下文的项目添加到相应的导航集合中。要执行此操作(对现有代码进行最少的更改),请从AddProject
:
public static Project AddProject(this IEnumerable<Project> projects,
Projects newProject, ref DataContext ctx)
{
var project = new Project { ... };
ctx.Projects.Add(project);
return project;
}
然后将此返回的项目传递到AddPerson
:
var project = _ctx.Projects.AddProject(newProject, ref _ctx);
_ctx.Person.AddPerson(newProject.Person, project, ref _ctx);
_ctx.SaveChanges();
并在AddPerson
中将其添加到此人的Projects
集合中:
public static void AddPerson(this IEnumerable<Person> persons, Person newPerson,
Projects project, ref DataContext ctx)
{
var person = new Person { ... };
if (project != null)
person.Projects.Add(project);
// the preceding line tells EF about the relationship and will make it
// generate an INSERT statement for the link table ProjectPerson
ctx.Person.Add(person);
}