问题描述
这是我第一次使用 EF5,我不太清楚如何执行此操作: 我有以下数据库架构: schema
我需要创建一个返回(实际上我需要一个 CRUD)设备(来自 Devices 表)及其属性(来自其他 3 个表)的端点 但是端点发送了错误:
Unable to determine the relationship represented by navigation 'Device.DeviceDevicePropertyValues' of type 'ICollection<DeviceDevicePropertyValue>'. Either manually configure the relationship,or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
问题是我不知道在哪里手动配置relationShip;我没有使用“OnModelCrating”方法或那些东西。
这是我的代码:
设备实体:
[Table("Devices")]
public class Device
{
[Key]
[required(ErrorMessage = "DevicesID is required")]
public int deviceid { get; set; }
[required(ErrorMessage = "Name is required")]
[StringLength(30,ErrorMessage = "Name can't be longer than 30 characters")]
public string Name { get; set; }
[ForeignKey(nameof(Shop))]
public int ShopID { get; set; }
[ForeignKey(nameof(User))]
public int GroupID { get; set; }
public ICollection<DeviceDevicePropertyValue> DeviceDevicePropertyValues { get; set; }
}
DeviceDevicePropertyValue 实体
[Keyless]
[Table("Device_DevicePropertyValue")]
public class DeviceDevicePropertyValue
{
public DeviceDevicePropertyValue()
{
}
[ForeignKey(nameof(Device))]
[required(ErrorMessage = "deviceid is required")]
public int deviceid { get; set; }
[ForeignKey(nameof(DevicePropertyValue))]
[required(ErrorMessage = "DevicePropertyValueID is required")]
public int DevicePropertyValueID { get; set; }
}
DevicePropertyValue 实体
[Table("DevicePropertyValues")]
public class DevicePropertyValue
{
[Key]
[required(ErrorMessage = "DevicePropertyValueID is required")]
public int DevicePropertyValueID { get; set; }
[required(ErrorMessage = "Name is required")]
[StringLength(30,ErrorMessage = "Name can't be longer than 30 characters")]
public string Name { get; set; }
[ForeignKey(nameof(DevicePropertyField))]
[required(ErrorMessage = "DevicePropertyFieldID is required")]
public int DevicePropertyFieldID { get; set; }
}
DevicePropertyField 实体
[Table("DevicePropertyFields")]
public class DevicePropertyField
{
[Key]
[required(ErrorMessage = "DevicePropertyFieldID is required")]
public int DevicePropertyFieldID { get; set; }
[required(ErrorMessage = "Name is required")]
[StringLength(30,ErrorMessage = "Name can't be longer than 30 characters")]
public string Name { get; set; }
}
在 DeviceRepository 上我有:
public async Task<IEnumerable<Device>> GetAllDevicesinformation()
{
return await FindAll()
.OrderBy(device => device.deviceid)
.Include(deviceInfo => deviceInfo.DeviceDevicePropertyValues)
.ToListAsync();
}
在 RepostoryBase 我有:
public IQueryable<T> FindAll()
{
return this.RepositoryContext.Set<T>().AsNoTracking();
}
最后,在我的 DeviceController 中,我有这个:
[HttpGet("all")]
public async Task<IActionResult> GetAllDevicesinformation()
{
try
{
var devices = await _repository.Device.GetAllDevicesinformation();
_logger.LogInfo($"Returned all devices information from database.");
var devicessResult = _mapper.Map<IEnumerable<DeviceDto>>(devices);
return Ok(devicessResult);
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong inside GetAllDevicesinformation action: {ex.Message}");
return StatusCode(500,"Internal server error");
}
}
我正在 StackOverflow 上的其他帖子中搜索答案,但我不明白它或不适合我
非常感谢您的回答!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)