2

我正在尝试更新系统中的记录。模型上的所有东西都保存得很好,除了我在表单上的多对多类型关系中的任何一个。当我找到模型中的那些时,它给了我错误。“元数据集合中不存在身份为 '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();
        }
    });
}

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

4

1 回答 1

1

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

于 2016-05-31T17:44:27.943 回答