EF导航属性不起作用?

时间:2013-06-18 03:25:48

标签: c# .net entity-framework ef-code-first

我有一个卡类:

public class Card
{
    public Card(){}

    [Key]
    [Required]
    public virtual int CardId { get; set; }

    [ForeignKey("StageId")]
    public virtual Stage Stage { get; set; }
    public int StageId { get; set; }
    }
}

舞台课程:

public class Stage
{
    [Key]
    public virtual int StageId { get; set; }

    [DataMember(EmitDefaultValue = true)]
    public string Name { get; set; }

    public long Ticks { get; set; }

    [NotMapped]
    public TimeSpan Span
    {
        get { return TimeSpan.FromTicks(Ticks); }
    }

    // Make sure that these stages are generated accordingly
    public static class Ids
    {
        // Zero
        public const int One = 1;
        // Ten Seconds
        public const int Two = 2;
        // One Minute
    }

}

在我的存储库服务中,我有一个AddCard方法:

public Card AddCard(Card card)
    {
        int parentId = 0;
        Set parentSet = null;
        if (card.ParentSetId.HasValue)
        {
            parentId = card.ParentSetId.Value;
            parentSet = GetSet(parentId);
        }

        card.ParentSet = parentSet;
        card.ParentSetId = parentSet.SetId;

        card.StageId = Stage.Ids.One;  // Set Id here with hopes of getting card.Stage to resolve as a nav property

        _db.Cards.Add(card);

        SaveChanges();

        return card;
    }

但由于某种原因,我的所有卡片都有Card.Stage的空值 - 其中Card.StageId总是一些整数。我正在尝试使导航属性工作,所以我可以通过卡访问舞台的属性。

我在这里做错了什么?

更新:

当我尝试通过我的测试访问它时,该属性为null。首先我创建卡片(在我的测试代码中):

 var cardDto = new CardDto  // Works!
            {
                Details = "Test Card",
            };

然后我将它发送到我的Dto服务(也在测试代码中):

 var fullCardDto = _service.AddCard(cardDto); // breaks because Stage is null when stageId is not

CardDto类:

public class CardDto
{
    public CardDto(){}

    public CardDto(Card card)
    {
        CardId = card.CardId;
        Stage = card.Stage.Name;  // Fails here on its way back - creating the initial DTO works
        Details = card.Details;
    }

    [Key]
    [DataMember(EmitDefaultValue = true)]
    public int CardId { get; set; }


    [DataMember(EmitDefaultValue = true)]
    public string Stage { get; set; }

    [DataMember(IsRequired = false)]
    public string Details { get; set; }

    public Card ToEntity()
    {
        var newCard = new Card
            {
             CardId = CardId,
             Details = Details,
            };
        return newCard;
}

_service是DTO服务:

public CardDto AddCard(CardDto card)
    {
        return new CardDto(_repository.AddCard(card.ToEntity()));
    }

_repository代码在上面。

这一直在_repository调用中工作,但是如果我调试_repository.AddCard方法,StageId在调用SaveChanges()之后有一个值,但是Stage没有。

卡被传回链上,直到它应该再次转换为Dto,由于null阶段而失败。

奇怪的是,这段代码在测试之外工作 - 如果我使用客户端点击AddCard,我会收到一张没有任何错误的卡片。

1 个答案:

答案 0 :(得分:1)

要使Lazy加载工作,您需要创建一个代理。如果您正在加载卡 EF将自动接收包含在代理对象中的对象。你可以在这里做两件事 1.要使用延迟加载,您可以创建这样的代理,

public Card AddCard(Card card)
    {
        var cardProxy= _db.Cards.Create(); 
        //and copy all values from card to cardProxy here..
        int parentId = 0;
        Set parentSet = null;
        if (cardProxy.ParentSetId.HasValue)
        {
            parentId = cardProxy.ParentSetId.Value;
            parentSet = GetSet(parentId);
        }
        cardProxy.ParentSet = parentSet;
        cardProxy.ParentSetId = parentSet.SetId;

        cardProxy.StageId = Stage.Ids.One;  // Set Id here with hopes of getting card.Stage to resolve as a nav property

          SaveChanges();

        return cardProxy;
}
  1. 或者您可以使用显式加载

    公共卡AddCard(卡片卡)         {             int parentId = 0;             设置parentSet = null;             if(card.ParentSetId.HasValue)             {                 parentId = card.ParentSetId.Value;                 parentSet = GetSet(parentId);             }

            card.ParentSet = parentSet;
            card.ParentSetId = parentSet.SetId;
    
            card.StageId = Stage.Ids.One;  // Set Id here with hopes of getting card.Stage to resolve as a nav property
    
            _db.Cards.Add(card);
    
            SaveChanges();
    
            context.Entry(card).Reference(p => p.Stage).Load();//load stage here
    
           return card;
        }