我有一个Tools_NawContext类扩展DbContext和一个DbResult类,SaveChanges当发生异常时稍微调整方法的结果.当抛出异常时,我创建了一条特定的错误消息,我知道它属于我尝试添加,删除或编辑的一个实体.用户可以根据错误消息采取适当的操作,然后重试.
public partial class Tools_NawContext : DbContext
{
public Tools_NawContext(DbContextOptions<Tools_NawContext> options) : base(options) { }
public DbResult TrySaveChanges()
{
try {
int numberOfRowsSaved = SaveChanges();
return new DbResult(numberOfRowsSaved);
} catch(Exception ex) {
return new DbResult(ex);
}
}
}
public class DbResult
{
public DbResult(int numberOfRowsSaved) {
this.Succeeded = true;
this.NumberOfRowsSaved = numberOfRowsSaved;
}
public DbResult(Exception exception)
{
this.Exception = exception;
if(exception.GetType() == typeof(DbUpdateException) && exception.InnerException != null) {
if (exception.InnerException.Message.StartsWith("The DELETE statement …Run Code Online (Sandbox Code Playgroud) 我有以下类'schakeling',用EF生成,表示数据库表'schakeling'.在数据库中,'id'是主键,'plc_id'是外键.
public partial class schakeling
{
public schakeling()
{
this.verbruik = new HashSet<verbruik>();
}
public int id { get; set; }
public int plc_id { get; set; }
public string var_output { get; set; }
public string omschrijving { get; set; }
public int vermogen { get; set; }
public Nullable<bool> status { get; set; }
public Nullable<byte> hourOn { get; set; }
public Nullable<byte> minuteOn { get; set; }
public Nullable<byte> hourOff { get; set; }
public Nullable<byte> minuteOff …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个通用的repo并使用autofac进行测试.我有以下界面:
public interface IGenRepo<T, TKey> where T : class
{
IQueryable<T> Items { get; }
T find(TKey pk);
RepoResult delete(TKey pk);
RepoResult create(T item);
RepoResult update(T item);
RepoResult save();
}
Run Code Online (Sandbox Code Playgroud)
这是实现该接口的类:
public class EFGenRepo<T, TKey> : IGenRepo<T, TKey> where T : class
{
private PortalEntities context = new PortalEntities();
public IQueryable<T> Items { get { return context.Set<T>().AsQueryable<T>(); } }
public T find(TKey pk){}
public RepoResult delete(TKey pk){}
public RepoResult create(T item){}
public RepoResult update(T item){}
public RepoResult save(){}
private RepoResult …Run Code Online (Sandbox Code Playgroud) 我在使用带有构造函数属性的 DI 时遇到问题。我正在构建一个PDFBuilder基于我的IPDFBuilder.
public interface IPDFBuilder
{
string templatefilepath { get; }
string templatefilename { get; }
Dictionary<string, string> dict { get; }
void CreatePDF();
}
public class PDFBuilder : IPDFBuilder
{
public string templatefilename { get; private set; }
public string templatefilepath { get; private set; }
public Dictionary<string, string> dict { get; private set; }
public PDFBuilder(string templatefilename, string templatefilepath, Dictionary<string, string> dict)
{
this.templatefilename = templatefilename;
this.templatefilepath = templatefilepath;
this.dict = dict;
} …Run Code Online (Sandbox Code Playgroud) 我上传了一个带有 HTML 表单的 JSON 文件,如第一段中所述。我一次只接受 1 个文件,所以这是我的控制器:
public IActionResult Upload(IFormFile file)
{
}
Run Code Online (Sandbox Code Playgroud)
现在我想将包含 JSON 的文件转换为对象。就像Cuong Le这个被接受的答案一样。如何将文件转换为 Let's say MyObject?我如何反序列化文件?(Newtonsoft 是要导入的库吗?)