实体框架更新嵌套对象

时间:2015-01-20 17:02:49

标签: c# entity entity-framework-6

我正在尝试通过EF 6执行更新并遇到问题。我对EF比较新,所以如果问题有点暗淡,请原谅我。 当我尝试更新仪器表时出现以下错误: 实体类型HashSet`1不是当前上下文的模型的一部分。

我认为这是因为嵌套对象。有谁知道如何填充这些?

提前致谢。

请参阅我的更新和型号的以下代码。

        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 个答案:

答案 0 :(得分:1)

如果您使用Property的{​​{3}},您会立即发现错误。它看起来像

_entities.Entry(i.InstrumentAlias)
    .Property(x => x.InstrumentKey)
    .IsModified = false;

仔细观察,您会注意到intellisense没有显示x.InstrumentKey。为什么不?因为i.InstrumentAlias不是InstrumentAlias,而是ICollection<InstrumentAlias>。代码甚至都不会编译。

问题是Entry应该返回一个DbEntityEntry对象,一个包含有关一个实体类实例的EF模型信息的对象。实体类是当前上下文的模型的一部分

这是例外情况,i.InstrumentAlias是(ICollection已实施为)HashSetHashSet不是模型的一部分。

所以无论你想要在那里实现什么,都应该从

开始
_entities.Entry(i)

或者,如果您想将InstrumentKey集合中的所有InstrumentAlias标记为已修改,则应循环显示此集合:

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

旁注:曾经听说过AutoMapper?