我在NET Core2.0 App中有以下类.
// required when local database does not exist or was deleted
public class ToDoContextFactory : IDesignTimeDbContextFactory<AppContext>
{
public AppContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<AppContext>();
builder.UseSqlServer("Server=localhost;Database=DbName;Trusted_Connection=True;MultipleActiveResultSets=true");
return new AppContext(builder.Options);
}
}
Run Code Online (Sandbox Code Playgroud)
当Database不存在时,在Core 2.0中需要进行迁移,并且必须在运行update-database时创建.
升级到ASP.NET Core 2.0后无法创建迁移
我想在2个地方(这里和appsettings.json)没有ConnectionString,但只在.json,所以我试图更换
"Server=localhost;Database=DbName;Trusted_Connection=True;MultipleActiveResultSets=true"
Run Code Online (Sandbox Code Playgroud)
同
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我得到空值.
更新1:
请注意,在Core 2中明确添加.json是不必要的,所以问题不在于文件.
https://andrewlock.net/exploring-program-and-startup-in-asp-net-core-2-preview1-2/
更新2:
此外,我已经使用Configuration从.json向Context发送ConnectionString:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); …
Run Code Online (Sandbox Code Playgroud) 我正在使用动态 Linq 进行通用搜索。我有 Id 列表:
List<int> idList = new List<int> { 1, 5, 6};
Run Code Online (Sandbox Code Playgroud)
在普通的 Linq 中,我会写:
q = q.Where(a => idList.Contains(a.MyId));
Run Code Online (Sandbox Code Playgroud)
但是现在我必须使用,System.Linq.Dynamic
因为我事先不知道该列的名称。
string someId = "CustomId";
q = q.Where("@0"+ ".Contains(" + someId + ")", idList.ToArray());
Run Code Online (Sandbox Code Playgroud)
但这给出了错误:
“'Int32' 类型中不存在适用的方法 'Contains'”
我怎样才能做到这一点?
是否有一些Contains
为dynamic
Linq 或其他方式实现的扩展库。
我很好奇使用 EF Core 时 SQL 中的具体场景。例如,有一列允许 null,同时具有默认值,这是不寻常的情况,但这不是这里的问题。问题是关于技术可能性。
[SomeId] [uniqueidentifier] NULL DEFAULT (newsequentialid())
Run Code Online (Sandbox Code Playgroud)
在应用程序中有一个具有适当属性的实体(代码优先)
public Guid? SomeId { get; set; }
Run Code Online (Sandbox Code Playgroud)
问题是如何将此实体插入到数据库中,以便SomeId具有 Null 值。因为即使属性为 Null,数据库也会用默认值覆盖它。
这可以通过将 null 显式分配给列来使用纯 sql 来完成,但似乎使用 EF 无法完成相同的操作,还是我错了?
编辑:详细说明
public class ExampleTable
{
[Key]
int Id { get; set; }
string Name { get; set; }
Guid? SomeId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以种子方法为例:
var record1 = new ExampleTable
{
FirstName = "Carson"
}
var record2 = new ExampleTable
{
FirstName = "Carson",
SomeId = …
Run Code Online (Sandbox Code Playgroud) 我有一个 IQueryable 扩展方法:
public static void SomeExt<T>(this IQueryable<T> query, DbContext context) {...}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有某种方法可以从查询中获取 DbContext,以便可以删除 DbContext 参数,只留下:
public static void SomeExt<T>(this IQueryable<T> query) {...}
Run Code Online (Sandbox Code Playgroud)
我在 IQueryable 后面尝试过类似Access DataContext 的东西, 但它不起作用,得到零字段。
也有办法从 DbSet 获取它
你能从 DbSet 获取 DbContext 吗?
myDbSet.GetService<'ICurrentDbContext>().Context;
但这不是我需要的。我想从 Query 中获取它?
这是查询:
var q = context.Items.Where(a => a.StatusId = 1);
q.SomeExt(上下文);
与
q.SomeExt();
appsettings ×1
arraylist ×1
asp.net-core ×1
c# ×1
contains ×1
dbcontext ×1
dynamic ×1
linq ×1
null ×1
nullable ×1