确实的方式任何人知道或申请从派生的所有类的实现IEntityTypeConfiguration<>对DbContext在运行时?
似乎没有内置任何内容并通过以下方式手动加载每个内容:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new Table1Config())
modelBuilder.ApplyConfiguration(new Table2Config())
...
modelBuilder.ApplyConfiguration(new TableNConfig())
}
Run Code Online (Sandbox Code Playgroud)
对于具有许多表的数据库来说,这将被证明是相当乏味的。
我正在使用ASP.NET Core 2.2,EF Core和MOQ。当我运行测试时,出现此错误:
消息:System.NotSupportedException:在非虚拟(在VB中可重写)成员上的无效设置:x => x.Movies
我做错了什么?
public class MovieRepositoryTest
{
private readonly MovieRepository _sut;
public MovieRepositoryTest()
{
var moviesMock = CreateDbSetMock(GetFakeListOfMovies());
var mockDbContext = new Mock<MovieDbContext>();
mockDbContext.Setup(x => x.Movies).Returns(moviesMock.Object);
_sut = new MovieRepository(mockDbContext.Object);
}
[Fact]
public void GetAll_WhenCalled_ReturnsAllItems()
{
//Act
var items = _sut.GetAll();
//Assert
Assert.Equal(3, items.Count());
}
private IEnumerable<Movie> GetFakeListOfMovies()
{
var movies = new List<Movie>
{
new Movie {Id = 1, Title = "Movie 1", YearOfRelease = 2018, Genre = "Action"},
new Movie {Id = 2, …Run Code Online (Sandbox Code Playgroud) 我有以下查询:
db.Users.AsQueryable()
.Where(u => u.Id = userResolver.LoggedUserId() && u.Packages.Where(p =>
p.StatusId == (int)PackageStatus.InProgress ||
p.StatusId == (int)PackageStatus.Delivered ||
p.StatusId == (int)PackageStatus.Shipped ||
p.StatusId == (int)PackageStatus.Waiting)
.Sum(p => p.Price) > u.MaxCredit)
.ToList()
Run Code Online (Sandbox Code Playgroud)
我想要实现的目标是将所有包状态检查分组到扩展方法中。像这样的东西:
db.Users.AsQueryable()
.Where(u => u.Id = userResolver.LoggedUserId() &&
u.Packages.Where(p => p.IsShippedOrInProgress())
.Sum(p => p.Price) > u.MaxCredit)
.ToList()
//This is the extension method
public static bool IsShippedOrInProgress(this Package p) {
return p.StatusId == (int)PackageStatus.InProgress ||
p.StatusId == (int)PackageStatus.Delivered ||
p.StatusId == (int)PackageStatus.Shipped ||
p.StatusId == (int)PackageStatus.Waiting)
}
Run Code Online (Sandbox Code Playgroud)
当我查看第一个示例中生成的 sql 查询时,一切似乎都正常,但是当我使用第二种方法时,检查状态的查询部分不存在。
为了制作多行输入字段,像ASP.NET MVC一样,我尝试了以下操作,但在ASP.NET Core MVC中不起作用:
public class Post
{
[DataType(DataType.MultilineText)]
public string Body { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在视图中:
<input asp-for="Body" rows="40" class="form-control"/>
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激!
如果我使用.NET Core 2.1和一些典型的POST和GET方法创建API,那么这些方法的返回类型是最合适的,ActionResult<T>还是async Task<T>?我的一个朋友在他创建的每个API中都使用后者,这就是我所在的公司所使用的,而PluralSite上的教程使用前者.我理解每个人做了什么,但我不确定哪个应该为任何给定的HTTP请求实现?
我想在 asp.mvc core 3.X 中更改 .AspNetCore.Antiforgery.xxx 的默认 Cookie 名称,但是我似乎没有找到任何关于它的文档。甚至有可能吗?
我发现唯一可以改变的是:
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
options.ConsentCookie.Name = "GDRP";
});
Run Code Online (Sandbox Code Playgroud) 在中EntityFramework 6.x,如果我们有很多EntityConfiguration类,则可以OnModelCreating(ModelBuilder modelBuilder)按以下方式分配所有类,而不是一个接一个地分配:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.AddFromAssembly(typeof(MyDbContext).Assembly);
}
Run Code Online (Sandbox Code Playgroud)
是否有任何类似 modelBuilder.Configurations.AddFromAssembly 的 实体框架核心。
谢谢。
如果我使用新引入的InProcess托管模型ASP.NET Core 2.2如下:
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
Serilog不写日志到文件.但是,如果我<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>从.csproj一切中删除按预期工作.
我Serilog在Program课堂上的配置如下:
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information() // Set the minimun log level
.WriteTo.File("Logs\\log-.txt", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7) // this is for logging into file system
.CreateLogger();
try
{
Log.Information("Starting web host");
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
}
public static …Run Code Online (Sandbox Code Playgroud) 我已经加入DateCreated,UserCreated,DateModified并UserModified通过创建一个字段每个实体BaseEntity.cs类。我的要求是生成单个表,该表将base类的所有字段作为列。
public class BaseEntity
{
public DateTime? DateCreated { get; set; }
public string UserCreated { get; set; }
public DateTime? DateModified { get; set; }
public string UserModified { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的Student课
public class Student : BaseEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的上下文类
public class SchoolContext: DbContext
{
public LibraContext(DbContextOptions<SchoolContext> options)
: …Run Code Online (Sandbox Code Playgroud) c# migration entity-framework database-migration entity-framework-core
这是我的聊天javascript
"use strict";
var connection = new signalR.HubConnectionBuilder().withUrl("/chathub").build();
connection.on("ReceiveMessage", function (message) {
var msg = message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
var encodedMsg = msg;
var li = document.createElement("li");
li.textContent = encodedMsg;
document.getElementById("Messages").appendChild(li);
});
connection.start().catch(function (err) {
return console.error(err.toString());
});
document.getElementById("Send").addEventListener("click", function (event) {
var message = document.getElementById("Message").value;
connection.invoke("SendMessage", message).catch(function (err) {
return console.error(err.toString());
});
event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
这是我使用聊天输入运行页面时遇到的错误:
未捕获的ReferenceError:在chat.js:3中未定义signalR
第3行chat.js是:
var connection = new signalR.HubConnectionBuilder().withUrl("/chathub").build();
Run Code Online (Sandbox Code Playgroud)
我SignalR从Visual Studio添加客户端库下载了客户端库。我现在有一个名为文件jquery.signalR.js,它是ASP.NET SignalR JavaScript库2.4.0。
但是,此错误不会消失,由于某些原因,我无法继续。
c# ×8
asp.net-core ×7
asp.net ×2
ef-core-2.1 ×1
html ×1
javascript ×1
jquery ×1
migration ×1
moq ×1
serilog ×1
signalr ×1
unit-testing ×1