/// <summary> /// 自动处理附件, 处理对象为包含附件外键的数据模型, 比如, 处理客户沟通的附件, 则参数对象 obj 应为 CustomerCommunicateAttachment 类型对象 /// </summary> /// <param name="ctx">上下文</param> /// <param name="obj">类型实例</param> public static void AddNewAttachmentData(this DbContext ctx, object obj) { var type = obj.GetType(); var foreignKey = type.GetForeignKey(WebConfig.AttachmentEntitySetType); if (foreignKey.IsNotNullOrEmpty()) { var property = type.GetProperty(foreignKey); if (property != null) { var value = property.GetValue(obj); if ((value as IEntitySet).IsNullOrEmpty()) { var fileName = obj.GetPropertyValue(nameof(EntitySetWithAttachment.FileName)); if (fileName != null) { var att = WebConfig.GetAttachmentFromSession(fileName.ToString()); if (att != null) { ctx.Save(att); property.SetValue(obj, (att as IEntitySet).Id); } } } } } }