小编Kip*_* ei的帖子

添加一批实体.调用SaveChanges()时如何确定哪些实体失败

我有一个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)

c# entity-framework-core asp.net-core-mvc

6
推荐指数
1
解决办法
539
查看次数

如何确定PropertyType是否为外键

我有以下类'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)

c# asp.net-mvc entity-framework html-helper foreign-keys

5
推荐指数
1
解决办法
2296
查看次数

Autofac:如何使用多个类型参数注册泛型?使用EFGenRepo <T,TKey>注册IGenRepo <T,TKey>

我正在尝试构建一个通用的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)

c# autofac entity-framework-6 .net-4.5.2 asp.net-mvc-5.2

4
推荐指数
1
解决办法
1524
查看次数

使用动态构造函数注入的 DI

我在使用带有构造函数属性的 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)

c# dependency-injection constructor-injection

2
推荐指数
1
解决办法
744
查看次数

将 IFile(JSON 文件)转换为 MyObject

我上传了一个带有 HTML 表单的 JSON 文件,如第一段中所述。我一次只接受 1 个文件,所以这是我的控制器:

public IActionResult Upload(IFormFile file)
{
}
Run Code Online (Sandbox Code Playgroud)

现在我想将包含 JSON 的文件转换为对象。就像Cuong Le这个被接受的答案一样。如何将文件转换为 Let's say MyObject?我如何反序列化文件?(Newtonsoft 是要导入的库吗?)

c# json.net asp.net-core-mvc

1
推荐指数
1
解决办法
4077
查看次数