I\xe2\x80\x99m 尝试使用 Azure Key Vault 存储实体框架的 Web api 连接字符串。理想情况下,我\xe2\x80\x99d 希望避免将 Key Vault nuget 包与我的数据访问代码耦合。我的 dbContext 类有两个构造函数:
\n\npublic MyDbContext() : base("DefaultConnection")\n{ . . . }\n\npublic MyDbContext(string connectionString) : base(connectionString)\n{ . . . }\nRun Code Online (Sandbox Code Playgroud)\n\n我的代码使用无参数构造函数,它从 Web 配置获取连接字符串。在某些地方,我实例化了一个新的 MyDbContext 对象,这禁止使用注入的解决方案。
\n\n我采取的方法是使用连接字符串定位器在 dbcontext 上设置静态属性:
\n\npublic interface IConnectionStringLocator\n{ string Get(); }\n\npublic class DefaultConnectionStringLocator : IConnectionStringLocator\n{\n public string Get()\n {\n return "DefaultConnection";\n }\n}\n\npublic static IConnectionStringLocator ConnectionStringLocator { get; set; } =\n new DefaultConnectionStringLocator();\nRun Code Online (Sandbox Code Playgroud)\n\n我的 Web api 项目有用于检索密钥保管库机密的 nuget 包。所以在我的 Global.asax 文件中我有这个: …
entity-framework azure azure-keyvault azure-managed-identity
我正在使用 AutoMapper ProjectTo 将 Person 实体映射到 EmployeeDto。在我的 EmployeeDto 中,我有一个想要捕获的 AddressDto 属性。因为这个人有一个地址集合,我定义了我的映射来捕获第一个:
.ForMember(d => d.Address, s => s.MapFrom(m => m.Addresses.FirstOrDefault())
Run Code Online (Sandbox Code Playgroud)
问题是,当我将它包含在我的映射中时,我收到一个错误,指出“不支持指定类型成员“IsDeleted”...”IsDeleted 是具有 [NotMapped] 属性的 Person 的基本属性。我的 EmployeeDto 不包括 IsDeleted 属性。
当我查看 IQueryable 表达式属性时,我得到了这个:
System.Data.Entity.Core.Objects.ObjectQuery`1[MyProject.Data.Core.Entities.Person]
.MergeAs(MergeOption.AppendOnly)
.Where(
// Quoted to induce a closure:
a => a.FirstName.Contains(param.Value))
.Select(
// Quoted to induce a closure:
dto => new 0_Culture=neutral_PublicKeyToken=null>
{
IsDeleted = dto.IsDeleted,
UpdateDate = dto.UpdateDate,
CreateDate = dto.CreateDate,
Id = dto.Id,
…remaining Person entity properties removed for brevity,
Addresses = dto.Addresses,
Address = …Run Code Online (Sandbox Code Playgroud)