我必须使用类,VisitModel和VisitorModel,visitModel可以有许多VisitorModel,反之亦然。将两个关系添加到modelBuilder后,它仍然没有保存到第三个自动生成的表。它创建了表,但是当我尝试保存时,它不会保存两个ID。这是我的代码:
public class VisitorModel
{
public int VisitorModelId { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public bool IsDeleted { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string PhotoUrl { get; set; }
public string Address { get; set; }
public int BranchId { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public virtual ICollection<VisitModel> VisitModels { get; set; }
}
这是VisitModel类:
public class VisitModel
{
public VisitModel()
{
VisitorModels = new List<VisitorModel>();
}
public int VisitModelId { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public bool IsDeleted { get; set; }
public string UserToVisit { get; set; }
public virtual User User { get; set; }
public string HostEmail { get; set; }
public int UserId { get; set; }
public int VisitorTypeId { get; set; }
public int PurposeOfVisitId { get; set; }
public string OtherPurposeOfVisit { get; set; }
public DateTime ArrivalDate { get; set; }
public DateTime ArrivalTime { get; set; }
public DateTime DepartureTime { get; set; }
public DateTime DepartureDate { get; set; }
public bool IsCheckedIn { get; set; }
public bool IsCheckedOut { get; set; }
public string BookedBy { get; set; }
public bool IamHost { get; set; }
public virtual ICollection<VisitorModel> VisitorModels { get; set; }
}
这是modelBuilder:
modelBuilder.Entity<VisitModel>()
.HasMany(up => up.VisitorModels)
.WithMany(course => course.VisitModels)
.Map(mc =>
{
mc.ToTable("VisitVisitorModel");
mc.MapLeftKey("VisitModelId");
mc.MapRightKey("VisitorModelId");
});
当我尝试保存时,它不会将两个ID保存到VisitVisitorModel表。该表始终为空。请问我做错了什么?
答案 0 :(得分:0)
因为您遵循entity framework many-to-many conventions,所以您不需要流畅的API部分来设计联结表。
实体框架通过查看您的类定义,知道您设计了多对多关系。实体框架将为您设计合适的联结表。您甚至不应该在dbContext中提及此联结表:
class MyDbContext : DbContext
{
public DbSet<VisitModel> VisitModels {get; set;}
public DbSet<VistitorModel> VistiroModels {get; set}
}
但如果我的联结表没有DbSet,我如何在VisitModel和VisitorModel上进行联接?
答案是:不要使用联接,使用ICollections;
向我提供未删除的所有VisitorModel的名称和地址以及今天已登记的VisitModel的所有到达时间和出发时间
通常,您会将VisitorModel与VisitModelId上的联结表VisitVisitorModel一起加入,并再次加入VisitorModelId上的VisitModel。
但你也可以使用ICollectioins:
var result = myDbContext.VisitorModels
.Where(visitorModel => !visitorModel.IsDeleted)
.Select(visitorModel => new
{
// select only the properties of visitorModel you will be using:
FullName = visitorModel.FullName,
Address = visitorModel.Address,
Phone = visitorModel.Phone,
... // other properties
// get ArrivalTime and DepartureTime of all VisitModels that are checked in
VisitModels = visitorModel.VisitModels
.Where(visitModel => visitModel.IsCheckedInd)
.Select(visitModel => new
{ // again: select only the properties you will be using:
ArrivalTime = visitModel.ArrivalTime,
DepartureTime = visitModel.DepartureTime,
... // other properties
})
.ToList(),
}
}
由于适当的多对多配置,您不需要联结表,并且您不需要使用主键和外键进行连接。实体框架将知道要做哪些连接。