【问题标题】:The member with identity 'PmData.SafetyRequirement_Assets' does not exist in the metadata collection.\r\nParameter name: identity元数据集合中不存在身份为“PmData.SafetyRequirement_Assets”的成员。\r\n参数名称: 身份
【发布时间】:2016-09-25 22:45:33
【问题描述】:

我正在尝试更新系统中的记录。模型上的所有内容都保存得很好,除了我在表单上的多对多类型关系中的任何一个。当我找到模型中的那些时,它给了我错误。 “元数据集合中不存在身份为 'PmData.SafetyRequirement_Assets' 的成员。\r\n参数名称:身份”。我已经阅读了其他一些答案,但我的数据库上没有任何触发器,并且我已经根据其他建议对我的模型进行了几次更改,但它似乎没有任何改变。该项目在 vNext 中。

这是我的第一个模型

public partial class Asset : DataModel
{
    [Required]
    [StringLength(64)]
    public string Name { get; set; }
    [StringLength(256)]
    public string Description { get; set; }
    [StringLength(1024)]
    public string SystemFunction { get; set; }
    [StringLength(2048)]
    public string Remarks { get; set; }
    public bool IsSystem { get; set; }
    public bool IsGrouping { get; set; }
    [StringLength(128)]
    public string FieldTag { get; set; }
    [ForeignKey("Parent")]
    public int? ParentId { get; set; }
    [ForeignKey("Building")]
    public int? BuildingId { get; set; }
    public bool IsOperable { get; set; }
    public bool IsAvailable { get; set; }
    public virtual Asset Parent { get; set; }
    public virtual Building Building { get; set; }
    public virtual ICollection<Asset> Children { get; set; }
    public virtual ICollection<DrawingReference> DrawingReferences { get; set; }
    public virtual ICollection<SpecReference> SpecReferences { get; set; }
    public virtual ICollection<SafetyRequirement> SafetyRequirements { get; set; }
    public virtual ICollection<SupportSystem> SupportSystems { get; set; }
}

具有多对多的另一个表的模型。

public partial class SafetyRequirement : DataModel
    {
        [StringLength(256)]
        [Required]
        public string Name { get; set; }
        [StringLength(2048)]
        public string SafetyFunction { get; set; }
        [StringLength(2048)]
        public string FunctionalRequirements { get; set; }
        [StringLength(2048)]
        public string SystemBoundary { get; set; }
        [StringLength(255)]
        public string Reference { get; set; }
        [ForeignKey("QualityLevel")]
        public int QualityLevelId { get; set; }
        public virtual QualityLevel QualityLevel { get; set; }
        public virtual ICollection<Asset> Assets { get; set; }
    }

连接表的地图

modelBuilder.Entity<Asset>().HasMany(t => t.SafetyRequirements)
    .WithMany(t => t.Assets)
    .Map(m =>
    {
       m.MapRightKey("SafetyRequirementId");
       m.MapLeftKey("AssetId");
       m.ToTable("AssetSafetyRequirement");
    });

最后,这是它失败的地方......

public virtual void SaveAsync(TEntity model)
{
    Task.Run(() =>
    {
        using (
            var dbContext =
                (TContext)
                Activator.CreateInstance(typeof (TContext),
                ConfigOptions == null ? ConfigService.ConnectionString : ConfigOptions.ConnectionString))
        {
            var dbSet = dbContext.Set<TEntity>();
            dbSet.Attach(model);
            dbContext.Entry(model).State = EntityState.Modified;
            dbContext.SaveChanges();
        }
    });
}

任何信息或指针将不胜感激。

【问题讨论】:

    标签: c# .net asp.net5


    【解决方案1】:

    您正在尝试同时使用 Fluent API 和数据注释来定义表之间的关系。删除其中一个。

    【讨论】:

    • 这成功了!把它拿出来重新运行我的迁移,它开始到位。
    猜你喜欢
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 2023-03-28
    • 2011-07-06
    • 1970-01-01
    相关资源
    最近更新 更多