【问题标题】:entity framework update nested objects实体框架更新嵌套对象
【发布时间】:2015-01-20 17:02:49
【问题描述】:

我正在尝试通过 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; }
}

【问题讨论】:

  • 当然,您的 sn-p 不必是代码的长城才能重现问题。您能否将其缩减为仍然存在问题的更小尺寸的东西?

标签: c# entity entity-framework-6


【解决方案1】:

如果您使用Property 的lambda version,您会立即发现有问题。它看起来像

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

仔细观察,您会注意到智能感知没有显示x.InstrumentKey。为什么不?因为i.InstrumentAlias 不是InstrumentAlias,而是ICollection&lt;InstrumentAlias&gt;。代码甚至无法编译。

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

所以这是个例外,i.InstrumentAlias 是一个(ICollection 实现为)HashSet 而HashSet 不是模型的一部分。

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

_entities.Entry(i)

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

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

旁注:听说过 AutoMapper 吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多