[英]EF6 Optional 1:1 include not working
Trying to set up basic EF6 Optional 1:1.尝试设置基本的 EF6 可选 1:1。 Extremely frustrating.
非常令人沮丧。 I have read lots and lost of posts.
我已经阅读了很多并且丢失了帖子。
I have two entities, Guests and Seats, each one can exist on their own, a seat does not need a guest and a guest does not need a seat.我有两个实体,Guests 和 Seats,每个实体都可以独立存在,一个座位不需要客人,客人不需要座位。
Guest Model:嘉宾模特:
public int ID { get; set; }
public int? SeatID { get; set; }
[Required]
public string FirstName { get; set; }
//Link to Seat
public Seat Seat { get; set; }
Seat Model:座椅型号:
public int ID { get; set; }
public int? GuestID { get; set; }
[Required]
public string SeatNumber { get; set; }
public bool Occupied { get { return (GuestID == null ? true : false); } }
//Link to Guest
public Guest Guest{ get; set; }
I had lots of problems just getting to this point.刚到这一步,我遇到了很多问题。 I had to add this to my model creation for the relationship to work:
我必须将此添加到我的模型创建中才能使关系起作用:
modelBuilder.Entity<Seat>()
.HasOptional(g => g.Guest)
.WithOptionalDependent(s => s.Seat);
Now my application is working however when I try and load related data with the include method it just comes up blank, nothing there.现在我的应用程序正在运行,但是当我尝试使用 include 方法加载相关数据时,它只是空白,什么都没有。
var seats = db.Seats.Include(s => s.Guest);
return View(seats.ToList());
On my view I use在我看来,我使用
@Html.DisplayFor(modelItem => item.Guest.FirstName)
I want to know how to make the include statement work, preferably without lazy loading (or even with if i have to).我想知道如何使 include 语句工作,最好不要延迟加载(或者即使我必须这样做)。 Also, I do not want to have a SeatAssignment table, I considered going down this road.
另外,我不想有一个 SeatAssignment 表,我考虑过这条路。
I could use a Viewmodel for this to work but i dont understand why the 1:1 optional is not loading the related data.我可以使用 Viewmodel 为此工作,但我不明白为什么 1:1 可选没有加载相关数据。
As requested, here is the generated schema...definitely something odd going on here.根据要求,这是生成的模式……这里肯定发生了一些奇怪的事情。 As a side note i can make it work with a viewmodel, but i dont want to.
作为旁注,我可以让它与视图模型一起工作,但我不想。
1-0..1 relationship configuration is different from the way that you want. 1-0..1 关系配置和你想要的方式不一样。
Based on the above mentioned rules, your code should be as follows根据上面提到的规则,你的代码应该如下
Guest class嘉宾班
public class Guest
{
public int ID { get; set; }
[Required]
public string FirstName { get; set; }
public IList<Seat> Seats { get; set; }
}
Seat class座位等级
public class Seat
{
public int ID { get; set; }
[Required]
public string SeatNumber{ get; set; }
public IList<Guest> Guests { get; set; }
}
Join table连接表
public GuestSeat
{
public int GuestID{get;set;}
public int SeatID{get;set;}
public DateTime ReservedDate{get;set;}
// other fields goes here
}
Configuration配置
modelBuilder.Entity<GuestSeat>()
.HasKey(gs => new {gs.GuestID,gs.SeatID})
.ToTable("GuestSeats");
modelBuilder.Entity<GuestSeat>()
.HasRequired(g=>g.Guest)
.WithMany(g=>g.Seats)
.HasForeignKey(gs =>gs.GuestID)
.DeleteOnCascade(false);
modelBuilder.Entity<GuestSeat>()
.HasRequired(s=>s.Seat)
.WithMany(s=>s.Guests)
.HasForeignKey(gs =>gs.SeatID)
.DeleteOnCascade(false);
This solution provide the following该解决方案提供以下
SeatID
and ReservedDate
.SeatID
和ReservedDate
创建唯一索引。 Hope this will help you希望能帮到你
You have to construct your models as shown below.您必须构建您的模型,如下所示。
public class Guest
{
public int GuestId { get; set; }
[Required]
public string FirstName { get; set; }
//Link to Seat
public virtual Seat Seat { get; set; }
}
public class Seat
{
public int SeatId { get; set; }
[Required]
public string SeatNumber { get; set; }
public bool Occupied { get { return (GuestID == null ? true : false); }}
//Link to Guest
public virtual Guest Guest{ get; set; }
}
Fluent API mapping :流畅的 API 映射:
modelBuilder.Entity<Seat>()
.HasOptional(g => g.Guest)
.WithOptionalDependent(s => s.Seat);
Then :然后 :
var seats = db.Seats.Include(s => s.Guest).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.