TL; DR - 如何将身份验证添加到未使用auth启动的现有默认核心2 web api项目.
详细信息 - 我已经有一个现有的.net core 2 web api项目,没有配置身份验证,我正在使用实体框架核心.
它打开像 -
PIC 1 - 未选择身份验证
我想将Google身份验证添加到我现有的项目中,就像打开它一样
PIC 2 - 选择了单个用户帐户
但我找不到任何有关添加这些功能+脚手架和迁移的资源 - 我所能找到的是从核心v1升级到2的链接.
有任何想法吗?
谢谢!
在EF Core 2.0中,我们能够从IEntityTypeConfiguration
更清晰的Fluent API映射(源代码)派生.
如何扩展此模式以使用基础实体?在下面的示例中,我如何才能BaseEntityConfiguration
减少重复,LanguageConfiguration
并MaintainerConfiguration
修改BaseEntity
仅在BaseEntityConfiguration
?中的属性?这样的BaseEntityConfiguration
样子会是什么样的; 如果有的话,如何使用OnModelCreating()
?请参阅示例末尾附近的TODO代码.
例:
public abstract class BaseEntity
{
public long Id { get; set; }
public DateTime CreatedDateUtc { get; set; }
public DateTime? ModifiedDateUtc { get; set; }
}
public class Language : BaseEntity
{
public string Iso6392 { get; set; }
public string LocalName { get; set; }
public string Name { get; set; }
} …
Run Code Online (Sandbox Code Playgroud) c# entity-framework-core ef-fluent-api asp.net-core ef-core-2.0
如何使用注释或Fluent API指定列,表或数据库的排序规则?我知道MySql Provider有一些简洁的方法.但是,除了执行原始SQL命令之外,我找不到SQL Server的任何方法.
asp.net-mvc ef-code-first entity-framework-core ef-core-2.0 entity-framework-core-migrations
正如Entity Framework Indexing ALL foreign key columns 等问题中所述,EF Core 似乎自动为每个外键生成一个索引。这对我来说是一个合理的默认值(让我们不要在这里进行意见战......),但在某些情况下,这只是浪费空间并减慢插入和更新的速度。我如何根据具体情况预防它?
我不想完全关闭它,因为它利大于弊;我不希望有手动进行配置,所有这些指标我做想。我只是想在特定的FK上阻止它。
相关的问题:EF 文档中的任何地方都提到了自动创建这些索引的事实吗?我在任何地方都找不到它,这可能是我找不到如何禁用它的原因?
有人肯定会质疑我为什么要这样做......所以为了节省时间,链接问题的操作员在评论中给出了一个很好的例子:
例如,我们有一张
People
桌子和一张Addresses
桌子。该People.AddressID
FK是由EF索引,但我只从开始People
行和搜索Addresses
记录; 我从来没有找到Addresses
一行然后在该People.AddressID
列中搜索匹配的记录。
我有以下代码,导致System.ArgumentException
:
已添加具有相同键的项目.重点:PH
_HotelsByCountry = db.Hotels
.GroupBy(hotel => hotel.CountryCode)
.ToDictionary(group => group.Key, group => group.ToList());
Run Code Online (Sandbox Code Playgroud)
这是否意味着组密钥是不唯一的在使用时GroupBy
操作?
更新 hotel.CountryCode
是类型string
.
更新 CountryCode
是外键.
更新 sql server和ef core 2.0
更新以下代码的工作原理
_HotelsByCountry = db.Hotels
.GroupBy(hotel => hotel.CountryCode.Trim())
.ToDictionary(group => group.Key, group => group.ToList());
Run Code Online (Sandbox Code Playgroud) 我使用.net核心在Azure Functions中使用EF core 2.0.我正在尝试从local.settings.json读取db ConnectionString,其定义如下:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true"
},
"ConnectionStrings": {
"MyDbConnStr": "Data Source=.;Initial Catalog=xxxxxxx"
}
}
Run Code Online (Sandbox Code Playgroud)
Environment.GetEnvironmentVariable()不返回任何连接字符串信息,我也不能使用带有.net核心的ConfigurationManager.ConnectionStrings.如何从代码中访问连接字符串?
有一个方案来执行存储过程并读取EF Core中的返回值,返回单个值.
我尝试使用此代码,但这不起作用.我理解这ExecuteSqlCommand
对select不起作用,只能用于更新数据库.
var test = context.Database.ExecuteSqlCommand("SPName");
Run Code Online (Sandbox Code Playgroud)
存储过程只有一个select语句 Select 'somevalue'
寻找任何替代方法来获取存储过程返回的数据.
我编写了一些代码来制作动态表达式来过滤我的分页。我正在尝试制作 EF Core 内置函数的动态表达式以进行搜索 ( EF.Functions.Like
)。
我尝试过像bottom这样的方法,但它是一个扩展方法,调用该方法时不使用第一个参数。我不知道如何遵循==> Ef => Function => Like的方式。
该方法应该像这样使用=>Ef.Functions.Like("Property to search", "%Some Pattern")
var likeMethod = typeof(DbFunctionsExtensions)
.GetMethods()
.Where(p => p.Name == "Like")
.First();
string pattern = $"%{finalConstant}%";
ConstantExpression likeConstant = Expression.Constant(pattern,typeof(string));
// the member expression is the property expression for example p.Name
var likeMethodCall = Expression.Call(method: likeMethod, arguments: new[] { memberExpression, likeConstant });
var searchLambda = Expression.Lambda<Func<T, bool>>(likeMethodCall, parameter);
query = query.Where(searchLambda);
Run Code Online (Sandbox Code Playgroud)
但它抛出异常说
为调用方法“Boolean Like(Microsoft.EntityFrameworkCore.DbFunctions, System.String, System.String)”提供的参数数量不正确\r\n参数名称: 方法
c# .net-core asp.net-core-webapi asp.net-core-2.0 ef-core-2.0
我对这个主题进行了广泛的搜索,并发现我想要做的事情的结果为零。
从高层次来看,这就是我想做的:
这有什么意义呢?
嗯,上面的断言已经描述得差不多了;数据的完整性。此项目中的迁移是在应用程序启动时使用Database.Migrate()
. 我想确保现有数据没有丢失/损坏。
我遇到的所有集成测试示例几乎都Database.Migrate()
作为测试设置的一部分运行,然后进行播种,然后进行断言。然而,这只适用于测试给定最新架构的数据访问层(已应用所有迁移)。它对于测试特定迁移对已存在数据的影响没有用处。
问题:
其他人如何解决跨迁移测试数据完整性的问题?我正在寻找一种能够与 CI 管道配合良好的设置。
integration-testing asp.net-web-api ef-core-2.0 entity-framework-migrations
所以我用这样的用户和角色来播种我的数据库。
public static void SeedUsers(this ModelBuilder modelBuilder)
{
var roles = new[]
{
new Role
{
Id = new Guid("5127599a-e956-4f5e-9385-1b8c6a74e4f1"),
RoleName = "Customer"
},
new Role
{
Id = new Guid("8634c476-20fa-4391-b8f7-8713abf61af0"),
RoleName = "Admin"
}
};
// customer password
byte[] customerPasswordHash = null;
byte[] customerPasswordSalt = null;
HashPassword("asd123", out customerPasswordHash, out customerPasswordSalt);
// admin password
byte[] adminPasswordHash = null;
byte[] adminPasswordSalt = null;
HashPassword("asd123", out adminPasswordHash, out adminPasswordSalt);
var users = new[]
{
new Users()
{
Id = new Guid("3b86f5a2-1978-46e3-a0b6-edbb6b558efc"), …
Run Code Online (Sandbox Code Playgroud) c# entity-framework-core .net-core ef-core-2.0 entity-framework-migrations
ef-core-2.0 ×10
c# ×7
.net-core ×3
asp.net-core ×3
entity-framework-migrations ×2
asp.net-mvc ×1
azure ×1
entity-framework-core-migrations ×1
linq ×1