【问题标题】:Entity Framework with Interface带接口的实体框架
【发布时间】:2018-09-22 04:31:45
【问题描述】:

我知道正常的编程。但现在我想实现接口。

我的问题是,如果我有一个 MVC 架构,接口应该只有控制器还是模型?

为什么是接口?因为我应该为另一家公司开发控制器,他们应该能够使用这些空洞的方法。

如果模型也应该有接口,那么用EntityFramework添加怎么会出现这个错误:

public async Task<Result> CreateOrderingEquipmentAsync(IOrderingEquipment orderingEquipment)
    {
        var result = new Result();

        if (!ModelStateIsValid(orderingEquipment))
        {
            return BadRequest(orderingEquipment);
        }

        if (await GetOrderingEquipmentAsync(orderingEquipment.Guid) != null)
        {
            return AlreadyExist(orderingEquipment.Guid);
        }

        try
        {
            orderingEquipment.Timestamp = DateTime.UtcNow;
            _context.OrderingEquipments.Add(orderingEquipment);
            await _context.SaveChangesAsync();
            result = CreatedResult(orderingEquipment);
        }
        catch (Exception e)
        {
            Log.Error(e);

            if (e.InnerException != null)
            {
                Log.Error(e.InnerException.Message);
            }
        }

        return result;
    }

【问题讨论】:

  • 天哪,请让截图更具可读性并添加引用代码
  • 不要包含代码的屏幕截图 - 包含代码本身。如果您包含其他内容的图像,请确保正确裁剪它。
  • 我截图显示错误信息
  • 您可以每次点击放大屏幕截图

标签: c# .net entity-framework interface


【解决方案1】:

您的 add 方法接受 OrderingEquipment 类型的对象,但您的变量是 IOrderingEquipment 类型。

尝试使用“as”关键字进行转换,然后在检查是否为 null 后,将新创建的变量传递给您的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多