0

in EF Database First when change ForeignKey(CommodityGroupID) automatic Get CommodityGroup for Commodity object, But in EF Code First(4.3.1) not doing.

public class Commodity  
        {
            public int CommodityID { get; set; }
            public string MadeBy { get; set; }
            public decimal ServiceTimePrice { get; set; }
            public decimal QCPrice { get; set; }
            public int ServicePoint { get; set; }
            public string Note { get; set; }
            public int CommodityGroupID { get; set; }
            [ForeignKey("CommodityGroupID")]
            public virtual CommodityGroup CommodityGroup { get; set; }
    }
    public class CommodityGroup
    { 
        public int CommodityGroupID { get; set; }
        public string CommodityGroupName { get; set; } 
        public virtual ICollection<Commodity> Commodities { get; set; }

    }

this Property defined in Edmx file (database first), i Should define this code in ef code first?

[BrowsableAttribute(false)]
[DataMemberAttribute()]
public EntityReference<CommodityGroup> CommodityGroupReference
{
    get
    {
        return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<CommodityGroup>("GaamRepairModel.FK_Commodity_CommodityGroup", "CommodityGroup");
    }
    set
    {
        if ((value != null))
        {
            ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<CommodityGroup>("GaamRepairModel.FK_Commodity_CommodityGroup", "CommodityGroup", value);
        }
    }
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Hono
  • 1

1 Answers1

0

It sounds like you're wanting a change tracking proxy. You want the CommodityGroup navigation property to update automatically when the FK is changed correct?

See this post on MSDN for details about the change tracking proxy.

This post on MSDN shows some code on how to test of your proxy object is being created properly.

Is this a new object? If so, you'll need to call the CreateObject function on your DbSet, not use the New Commodity().

Mark Oreta
  • 10,346
  • 1
  • 33
  • 36