问题
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