如何将 Entity Framework 与 RootObject DTO 一起使用?
How do I use Entity Framework with a RootObject DTO?
我正在尝试通过 Entity Framework 构建 table 或 tables,但是,我在使用我构建的 DTO 文件时遇到了问题来自我正在使用的 API。
这是我正在使用的 DTO 文件,我从 POSTMAN 为 JSON 数据特别粘贴了它。
public class AllRequests
{
public class Rootobject
{
public Operation operation { get; set; }
}
public class Operation
{
public Result result { get; set; }
public Detail[] details { get; set; }
}
public class Result
{
public string message { get; set; }
public string status { get; set; }
}
public class Detail
{
public string requester { get; set; }
public string workorderid { get; set; }
public string accountname { get; set; }
public string createdby { get; set; }
public string subject { get; set; }
public string technician { get; set; }
public string isoverdue { get; set; }
public string duebytime { get; set; }
public string priority { get; set; }
public string createdtime { get; set; }
public string ignorerequest { get; set; }
public string status { get; set; }
}
}
正如您在上面看到的那样,它非常分层,因此不像从简单的 class 文件生成 table 那样直接。
这是我的 DbContext
文件:
public class TransitionContext : DbContext
{
private const string connectionString = (connection);
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(connectionString);
}
public DbSet<AllRequests> Requests { get; set; }
}
我已经设置了 运行 的迁移,可以连接到我的 SQL 服务器。但是,当我在 PM 控制台中 运行 命令 Update-Database
时,它不起作用。错误不是直截了当的,但它们让我 运行 绕圈子,首先它设置了一个 ID 键,然后是 table 关系,但是没有关于如何做的好来源 material它。
谁能教我如何通过 Entity Framework 在我的 SQL 服务器上成功实施此 DTO?我猜它会利用模型创建器,但我仍然不确定该过程在这种情况下是如何完全运作的。
如有任何帮助,我们将不胜感激。
按照错误消息的建议尝试使用 [NotMapped]
public class Operation
{
public Result result { get; set; }
[NotMapped]
public Detail[] details { get; set; }
}
如果您需要该数组...EF 需要一种导航方式..即一个键。
我正在尝试通过 Entity Framework 构建 table 或 tables,但是,我在使用我构建的 DTO 文件时遇到了问题来自我正在使用的 API。
这是我正在使用的 DTO 文件,我从 POSTMAN 为 JSON 数据特别粘贴了它。
public class AllRequests
{
public class Rootobject
{
public Operation operation { get; set; }
}
public class Operation
{
public Result result { get; set; }
public Detail[] details { get; set; }
}
public class Result
{
public string message { get; set; }
public string status { get; set; }
}
public class Detail
{
public string requester { get; set; }
public string workorderid { get; set; }
public string accountname { get; set; }
public string createdby { get; set; }
public string subject { get; set; }
public string technician { get; set; }
public string isoverdue { get; set; }
public string duebytime { get; set; }
public string priority { get; set; }
public string createdtime { get; set; }
public string ignorerequest { get; set; }
public string status { get; set; }
}
}
正如您在上面看到的那样,它非常分层,因此不像从简单的 class 文件生成 table 那样直接。
这是我的 DbContext
文件:
public class TransitionContext : DbContext
{
private const string connectionString = (connection);
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(connectionString);
}
public DbSet<AllRequests> Requests { get; set; }
}
我已经设置了 运行 的迁移,可以连接到我的 SQL 服务器。但是,当我在 PM 控制台中 运行 命令 Update-Database
时,它不起作用。错误不是直截了当的,但它们让我 运行 绕圈子,首先它设置了一个 ID 键,然后是 table 关系,但是没有关于如何做的好来源 material它。
谁能教我如何通过 Entity Framework 在我的 SQL 服务器上成功实施此 DTO?我猜它会利用模型创建器,但我仍然不确定该过程在这种情况下是如何完全运作的。
如有任何帮助,我们将不胜感激。
按照错误消息的建议尝试使用 [NotMapped]
public class Operation
{
public Result result { get; set; }
[NotMapped]
public Detail[] details { get; set; }
}
如果您需要该数组...EF 需要一种导航方式..即一个键。