【发布时间】:2021-01-11 14:59:20
【问题描述】:
我目前正在使用一个收集 API 数据的包 (Crayon API NuGet Package),但是,我正在努力通过实体框架来实现它。我正在从 Crayon 中提取数据,我想将其存储到数据库中,代码很好,只是实体框架部分没有按我的意愿工作。当我运行迁移时,出现此错误:
“无法确定“Price”类型的导航“BillingStatement.TotalSalesPrice”表示的关系。请手动配置关系,或使用“[NotMapped]”属性或使用“EntityTypeBuilder”忽略此属性。 'OnModelCreating' 中的忽略'。”
因此,从这个错误的外观来看,我需要配置关系,但我不确定如何通过元数据来做到这一点,因为 API 数据和类是在包(元数据)中设置的。但这是我正在使用的实体框架模型。
public class CrayonDbContext : DbContext
{
private const string connectionString = @"myserver";
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(connectionString);
}
public DbSet<BillingStatement> BillingStatements { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AddressData>().HasNoKey();
modelBuilder.Entity<Price>().HasNoKey();
}
}
这是它使用的元数据中的 BillingStatement 类。
namespace Crayon.Api.Sdk.Domain.Csp
{
public class BillingStatement
{
public BillingStatement();
public int Id { get; set; }
public Price TotalSalesPrice { get; set; }
public ObjectReference InvoiceProfile { get; set; }
public ObjectReference Organization { get; set; }
public DateTimeOffset StartDate { get; set; }
public DateTimeOffset EndDate { get; set; }
public ProvisionType ProvisionType { get; set; }
}
}
现在这里是之前显示的帐单类中引用的类。
public class Price
{
public Price();
public decimal Value { get; set; }
public string CurrencyCode { get; set; }
}
public class ObjectReference
{
public ObjectReference();
public int Id { get; set; }
public string Name { get; set; }
}
public enum ProvisionType
{
None = 0,
Seat = 1,
Usage = 2,
OneTime = 3,
Crayon = 4,
AzureMarketplace = 5
}
我知道,当涉及标准化时,它开始变得更加复杂。我该怎么办?我将如何配置关系?是否有任何好的源材料有助于元数据类,我可以在其中搭建表格并按预期存储数据?
我们将不胜感激。
【问题讨论】:
标签: c# sql-server entity-framework metadata