entity framework update nested objects

c# entity entity-framework-6


I am trying to perform an update through EF 6 and having trouble. I am relatively new to EF so please forgive me if the question is a bit dim. I am getting the following error When I try and update the instrument table: The entity type HashSet`1 is not part of the model for the current context.

I think this is because of the nested objects. Does anyone know how to populate these?

Thanks in advance.

please see the code below of my update and model.

            var i = (from x in _entities.Instrument
                     where x.InstrumentKey == id
                     select x).First();

            _entities.Entry(i.InstrumentAlias).Property("InstrumentKey").IsModified = false;
            _entities.Entry(i.InstrumentAlias).Property("InstrumentAliasKey").IsModified = false;

            i.AccountGroupCode = src.AccountGroupCode;
            i.AccountKey = src.AccountKey;
            i.AccountType = src.AccountType;
            i.BBGTicker = src.BBGTicker;
            i.BoardLot = src.BoardLot;
            i.BondPricingFactor = src.BondPricingFactor;
            i.CashInstrument = src.CashInstrument;
            i.ChinaShare = src.ChinaShare;
            i.ContractSize = src.ContractSize;
            i.CountryExposure = src.CountryExposure;
            i.CountryIncorporation = src.CountryIncorporation;
            i.CountryQuotation = src.CountryQuotation;
            i.CouponFrequency = src.CouponFrequency;
            i.CouponRate = src.CouponRate;
            i.CUSIP = src.CUSIP;
            i.ExpiryStyleKey = src.ExpiryStyleKey;
            i.IndustryClass = src.IndustryClass;
            i.InstrumentAlias = src.InstrumentAlias;
            //i.InstrumentKey = src.InstrumentKey;
            i.InstrumentLongName = src.InstrumentLongName;
            i.InstrumentMarketData = src.InstrumentMarketData;
            i.InstrumentName = src.InstrumentName;
            i.InstrumentSectorCustomer = src.InstrumentSectorCustomer;
            i.InstrumentTypeID = src.InstrumentTypeID;
            i.ISIN = src.ISIN;
            i.IssueDate = src.IssueDate;
            i.LastUpdatedDateTime = src.LastUpdatedDateTime;
            i.MaturityDate = src.MaturityDate;
            i.OptionType = src.OptionType;
            i.OTC = src.OTC;
            i.PricingCurrency = src.PricingCurrency;
            i.PrimaryExchange = src.PrimaryExchange;
            i.ReportGroupCode = src.ReportGroupCode;
            //i.SEDOL = src.SEDOL;
            //i.Status = src.Status;
            i.StrikePriceCurrency = src.StrikePriceCurrency;
            i.UnderlyingInstrumentKey = src.UnderlyingInstrumentKey;
            i.VotingRights = src.VotingRights;

            i.InstrumentAlias.Select(x => x.ExternalInstrumentKey = src.InstrumentAlias.Select(y => y.ExternalInstrumentKey).First());
            i.InstrumentAlias.Select(x => x.SourceID = src.InstrumentAlias.Select(y => y.SourceID).First());


public partial class Instrument
    public Instrument()
        InstrumentAlias = new HashSet<InstrumentAlias>();
        InstrumentMarketData = new HashSet<InstrumentMarketData>();
        InstrumentSectorCustomer = new HashSet<InstrumentSectorCustomer>();

    public int InstrumentKey { get; set; }

    public string InstrumentTypeID { get; set; }

    public string InstrumentName { get; set; }

    public string InstrumentLongName { get; set; }

    public string PricingCurrency { get; set; }

    public decimal? CouponRate { get; set; }

    [Column(TypeName = "date")]
    public DateTime? MaturityDate { get; set; }

    public string IndustryClass { get; set; }

    public string SEDOL { get; set; }

    public string ISIN { get; set; }

    public string CUSIP { get; set; }

    public int? CouponFrequency { get; set; }

    public decimal? ContractSize { get; set; }

    public decimal? BoardLot { get; set; }

    public string PrimaryExchange { get; set; }

    [Column(TypeName = "date")]
    public DateTime? IssueDate { get; set; }

    public decimal? BondPricingFactor { get; set; }

    public string Status { get; set; }

    public string CountryExposure { get; set; }

    public string CountryQuotation { get; set; }

    public string CountryIncorporation { get; set; }

    public int? ReportGroupCode { get; set; }

    public string StrikePriceCurrency { get; set; }

    public int? ExpiryStyleKey { get; set; }

    public string OptionType { get; set; }

    public string AccountType { get; set; }

    public string AccountGroupCode { get; set; }

    public int? UnderlyingInstrumentKey { get; set; }

    public DateTime LastUpdatedDateTime { get; set; }

    public int? AccountKey { get; set; }

    public bool? CashInstrument { get; set; }

    public bool? OTC { get; set; }

    public decimal? VotingRights { get; set; }

    public string ChinaShare { get; set; }

    public string BBGTicker { get; set; }

    public virtual ICollection<InstrumentAlias> InstrumentAlias { get; set; }

    public virtual ICollection<InstrumentMarketData> InstrumentMarketData { get; set; }

    public virtual ICollection<InstrumentSectorCustomer> InstrumentSectorCustomer { get; set; }
1/20/2015 5:11:13 PM

Popular Answer

If you'd use the lambda version of Property you'd immediately see something's wrong. It would look like

    .Property(x => x.InstrumentKey)
    .IsModified = false;

Looking closer, you'd notice that intellisense doesn't show x.InstrumentKey. Why not? Because i.InstrumentAlias is not an InstrumentAlias, but an ICollection<InstrumentAlias>. The code wouldn't even compile.

The problem is that Entry should return a DbEntityEntry object, an object containing EF model information about one entity class instance. An entity class is a class that is part of the model for the current context.

So that's the exception, i.InstrumentAlias is a (ICollection implemented as) HashSet and HashSet is not part of the model.

So whatever you're trying to achieve there, it should start with


Or if you want to mark all InstrumentKey in the InstrumentAlias collection as modified, you should loop through this collection:

foreach(var ialias in i.InstrumentAlias)
    _entities.Entry(ialias).Property(x => x.InstrumentKey).Modified = true;

Side note: ever heard of AutoMapper?

1/20/2015 9:57:29 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow