entity framework update nested objects

馋奶兔 提交于 2021-02-08 11:27:31

问题


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.

        try
        {
            var i = (from x in _entities.Instrument
                              .Include("InstrumentAlias")
                              .Include("InstrumentMarketData")
                     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());




            this._entities.SaveChanges();
        }


[Table("Instrument")]
public partial class Instrument
{
    public Instrument()
    {
        InstrumentAlias = new HashSet<InstrumentAlias>();
        InstrumentMarketData = new HashSet<InstrumentMarketData>();
        InstrumentSectorCustomer = new HashSet<InstrumentSectorCustomer>();
    }

    [Key]
    public int InstrumentKey { get; set; }

    [StringLength(12)]
    public string InstrumentTypeID { get; set; }

    [Required]
    [StringLength(40)]
    public string InstrumentName { get; set; }

    [Required]
    [StringLength(85)]
    public string InstrumentLongName { get; set; }

    [StringLength(3)]
    public string PricingCurrency { get; set; }

    public decimal? CouponRate { get; set; }

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

    [StringLength(30)]
    public string IndustryClass { get; set; }

    [StringLength(16)]
    public string SEDOL { get; set; }

    [StringLength(16)]
    public string ISIN { get; set; }

    [StringLength(30)]
    public string CUSIP { get; set; }

    public int? CouponFrequency { get; set; }

    public decimal? ContractSize { get; set; }

    public decimal? BoardLot { get; set; }

    [StringLength(10)]
    public string PrimaryExchange { get; set; }

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

    public decimal? BondPricingFactor { get; set; }

    [Required]
    [StringLength(1)]
    public string Status { get; set; }

    [StringLength(2)]
    public string CountryExposure { get; set; }

    [StringLength(2)]
    public string CountryQuotation { get; set; }

    [StringLength(2)]
    public string CountryIncorporation { get; set; }

    public int? ReportGroupCode { get; set; }

    [StringLength(3)]
    public string StrikePriceCurrency { get; set; }

    public int? ExpiryStyleKey { get; set; }

    [StringLength(4)]
    public string OptionType { get; set; }

    [StringLength(1)]
    public string AccountType { get; set; }

    [StringLength(3)]
    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; }

    [StringLength(1)]
    public string ChinaShare { get; set; }

    [StringLength(30)]
    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:


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

_entities.Entry(i.InstrumentAlias)
    .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

_entities.Entry(i)

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?



来源:https://stackoverflow.com/questions/28051083/entity-framework-update-nested-objects

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