如何将模型映射到数据库



每当我尝试调试我的。net Core 5时,我得到这个错误-

类型为'System '的未处理异常。InvalidOperationException"发生在System.Private.CoreLib.dll中:'属性的报告。无法映射活动id,因为它是类型'int[]',它不是支持的基本类型或有效实体类型。显式映射此属性,或者使用'[NotMapped]'属性或使用'EntityTypeBuilder。忽略的"OnModelCreating"。

不知道如何修复它,但我有一个线索,错误从哪里来。

这是我的模型

public class Report : EntityBase
{
public string ReportName { get; set; }
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public string[] Locals { get; set; }
public string[] Regions { get; set; }
public int[] CampaignIds { get; set; }
public int[] AgentIds { get; set; }
public IList<Campaign> Campaigns { get; set; }
public IList<User> Agents { get; set; }
}

在OnModelCreating方法中,我有这个

modelBuilder.Entity<Report>().Property(x => x.Locals).HasConversion(y => string.Join(',', y), y => y.Split(',', System.StringSplitOptions.RemoveEmptyEntries));
modelBuilder.Entity<Report>().Property(x => x.Regions).HasConversion(y => string.Join(',', y), y => y.Split(',', System.StringSplitOptions.RemoveEmptyEntries))

我假设在这栋楼里,我需要将活动id映射到活动模型。

问题是我如何在数据库中保存campaign id和agentIds,现在只有本地和地区被保存。

为多对多关系创建的类。

public class ReportCampaign
{
public int Id { get; set; }
public Campaign Campaign { get; set; }
public Report Report { get; set; }
}

将模型更改为

public class Report : EntityBase
{
public string ReportName { get; set; }
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public string[] Locals { get; set; }
public string[] Regions { get; set; }
public IList<Campaign> Campaigns { get; } = new List<Campaign>();
public IList<User> Agents { get; } = new List<User>();
}

EF将在战役和用户实体上放置外键(阴影)属性。

最新更新