Navigation property no more working after migration of breeze 1.4.2

人走茶凉 提交于 2019-12-24 19:06:33

问题


I work with asp.net mvc with Durandal & breeze templates.

I have the following code-first classes:

1st scenario:

public class Transport
{
    [Key]
    public int Id { get; set; }
    ...
    public int? SenderId { get; set; }
    public virtual SendRecv Sender { get; set; }
}

public class SendRecv
{
    [Key]
    public int Id { get; set; }
    ...
    public virtual List<Transport> Transports { get; set; }
}

Then I can easily get all my related transports from my observable of type SendRecv:

if (sendRecvs()[i].transports().length > 0)
{
    ...
}

The problem: I add a new reference to the same entity SendRecv in my Transport entity like this:

2nd scenario:

public class Transport
{
    [Key]
    public int Id { get; set; }
    ...
    public int? SenderId { get; set; }  
    public int? ReceiverId { get; set; }
    public virtual SendRecv Sender { get; set; }
    public virtual SendRecv Receiver { get; set; }
}

Then I cannot get my related transports anymore! The navigation property named transport from my SendRecv entity does not exists anymore.

This don't work anymore:

if (sendRecvs()[i].transports().length > 0)
{
    ...
}

PS: I'm pretty sure this worked before the migration of breeze to 1.4.2

Any idea?

Thanks.


回答1:


I'm assuming you only want one collection of Transport in SendRecv. If so, try this (using fluent API):

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
   modelBuilder.Entity<SendRecv>()
      .HasMany(c => c.Transports)
      .WithRequired(c => c.Sender)
      .WillCascadeOnDelete(false);

   modelBuilder.Entity<SendRecv>()
      .HasMany(c => c.Transports)
      .WithRequired(c => c.Receiver)
      .WillCascadeOnDelete(false);
}

Now, if you want 2 Transport lists (i.e. Transports1 and Transports2) then try:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
   modelBuilder.Entity<SendRecv>()
      .HasMany(c => c.Transports1)
      .WithRequired(c => c.Sender)
      .WillCascadeOnDelete(false);

   modelBuilder.Entity<SendRecv>()
      .HasMany(c => c.Transports2)
      .WithRequired(c => c.Receiver)
      .WillCascadeOnDelete(false);
}


来源:https://stackoverflow.com/questions/18847327/navigation-property-no-more-working-after-migration-of-breeze-1-4-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!