在数据库中保存一个有点复杂关系的模型时,我遇到了一些麻烦.
类定义是:
public abstract class EntityBase
{
public virtual Guid Id { get; set; }
}
public class LoanRequest : EntityBase
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public virtual Applicant Applicant1 { get; set; }
public virtual Applicant Applicant2 { get; set; }
}
public class Applicant
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public Guid LoanRequestId { get; set; }
[ForeignKey("LoanRequestId")]
public virtual LoanRequest LoanRequest { get; set; }
public virtual ICollection<MonthlyIncome> Incomes …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc entity-framework foreign-keys data-annotations
从数据库读取数据时出现此错误:
在前一个操作完成之前,在此上下文中启动了第二个操作。不保证任何实例成员都是线程安全的。
我有以下 ApplicationContext.cs:
public class ApplicationContext : Microsoft.EntityFrameworkCore.DbContext
{
public ApplicationContext(DbContextOptions<ApplicationContext> options)
: base(options)
{ }
public DbSet<MyClass> MyClasses{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
下面的 ApplicationContextFactory.cs
public class ApplicationContextFactory : IDesignTimeDbContextFactory<ApplicationContext>
{
public ApplicationContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<ApplicationContext>();
var connection = "myConnectionString";
builder.UseSqlServer(connection);
return new ApplicationContext(builder.Options);
}
}
Run Code Online (Sandbox Code Playgroud)
以下 ServiceLoader.cs(我在其中声明 DI):
public static class ServiceLoader
{
public static void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IRepository, Repository>();
var connection = "myConnectionString";
services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(connection));
}
}
Run Code Online (Sandbox Code Playgroud)
最后,以下存储库,其中抛出异常:
public class …Run Code Online (Sandbox Code Playgroud) 我正在为使用mongoDB c#驱动程序的DAL创建一些单元测试。问题是我有要测试的方法:
public async virtual Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> predicate)
{
return (await Collection.FindAsync(predicate)).ToList();
}
Run Code Online (Sandbox Code Playgroud)
并使用Moq嘲笑了这样的集合:
var mockMongoCollectionAdapter = new Mock<IMongoCollectionAdapter<Entity>>();
var expectedEntities = new List<Entity>
{
mockEntity1.Object,
mockEntity2.Object
};
mockMongoCollectionAdapter.Setup(x => x.FindAsync(It.IsAny<Expression<Func<Entity,bool>>>(), null, default(CancellationToken))).ReturnsAsync(expectedEntities as IAsyncCursor<Entity>);
Run Code Online (Sandbox Code Playgroud)
但是,如果expectedEntities as IAsyncCursor<Entity>为null,则测试无法正常工作。
模拟此方法并处理IAsyncCursor的最佳方法是什么?
我有三个班:
public class class1 {}
public class class2 : class1 {}
public class class3 : class1 {}
Run Code Online (Sandbox Code Playgroud)
和一个项目列表class1,但我想只获得类型的class2东西,如:
list = list.where(x=>x.classType == class2)
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是怎样的?
谢谢!
我有一个日期在日志文件中使用以下格式:"dMMyyHHmmss".
因为如果找到的格式不是那个,我想抛出一个异常,我正在使用DateTime.ParseExact.问题是我收到了一个FormatException,其中包含以下消息:
'String'.....'未被识别为有效的DateTime.
模拟这个的代码是:
var format = "dMMyyHHmmss";
var date = new DateTime(2018, 1, 1, 1, 1, 1);
var strDate = date.ToString(format);
date = DateTime.ParseExact(strDate, format, CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)
有什么想法我不能使用那种格式?
c# ×5
asp.net-mvc ×1
date ×1
date-parsing ×1
datetime ×1
dbcontext ×1
foreign-keys ×1
lambda ×1
mongodb ×1
moq ×1
unit-testing ×1
where-clause ×1