【问题标题】:The property X is of type Y which is not supported by current database provider属性 X 属于 Y 类型,当前数据库提供程序不支持
【发布时间】:2019-05-03 22:14:29
【问题描述】:

我真的是 EF 新手(使用 EF 核心 2.1)并且到目前为止一直在学习一堆教程,但现在我已经开始创建自己的数据库结构,并且在尝试向数据库中添加值时偶然发现:

private async Task<int> Insert()
{
    var address = new Address { AddressLine1 = "1 any street",  AddressLine2 = "", AddressLine3 = "", City = "Any city" };

    using (var context = new BranchContext())
    {
        context.Addresses.AddAsync(address);//ERROR HERE
        ....
    }
 }

我得到错误:

InvalidOperationException:属性“Branch.Address”属于“地址”类型,当前数据库提供程序不支持。更改属性 CLR 类型或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略该属性。

我创建了以下类:

public class Address
{
    public int Id { get; set; }
    public int Guid { get; set; }
    public DateTime CreatedOn { get; set; }
    public string Number { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string Town { get; set; }
    public string City { get; set; }
    public string Postcode1 { get; set; }
    public string Postcode2 { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
}

public class Branch
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
    public IEnumerable<EmployeeBranch> Employee { get; set; }
    public bool IsMain { get; set; }
}

public class Employee
{
    public int Id { get; set; }
    public string Guid { get; set; }
    public string EmailAddress { get; set; }
    public JobTitle JobTitle { get; set; }
    public DateTime DateOfBirth { get; set; }
    public IEnumerable<EmployeeBranch> Branches { get; set; }
    public Branch PrimaryBranch { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PreferredName { get; set; }
    public Salutations Salutation { get; set; }
}

public enum Salutations
{
    Mr = 1,
    Mrs = 2,
    Miss = 3,
    Ms = 4
}

public class EmployeeBranch
{
    public int BranchId { get; set; }
    public Branch Branch { get; set; }
    public int EmployeeId { get; set; }
    public Employee Employee { get; set; }
}

public class JobTitle
{
    public int Id { get; set; }
    public string Guid { get; set; }
    public string Name { get; set; }
}

然后我跑了:

Add-Migration init
Update-Database

创建了以下内容:

要明确这是一个运行时错误,我已经查看了几个线程在尝试更新其数据库 herehere 时出现此错误,但他们的讨论并没有为我指明正确的方向(也许我'我错过了明显的)。

我该如何解决这个问题?最好不要使用 Channing 结构,尽管如果有充分的理由我很乐意这样做

【问题讨论】:

    标签: entity-framework ef-core-2.1


    【解决方案1】:

    在引用导航属性而不是约束或类似属性中的映射属性的愚蠢情况下,也可能会弹出此错误:

    modelBuilder.Entity<TheEntity>().HasKey(ma => new { ma.SomeKey, ma.NavPropInsteadOfProp });
    

    很遗憾,错误不够明确,但暂时将错误的罪魁祸首注释掉,会引出源头。

    【讨论】:

    • 这是给我的。谢谢你在这里提到这一点。它帮助修复了我的
    • 谢谢,正是我的情况。你可能至少为我节省了 30 分钟
    • 绝对棒的建议。你只是为我节省了很多令人费解的时间。
    • 我也引用了导航属性...但不是用于定义键...当我尝试映射到特定列名时。
    【解决方案2】:

    假设:目标是使Address 成为Branch 的必需属性

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Branch>()
            .HasOne(s => s.Address)
            .WithMany()
            .IsRequired();
    }
    

    您可以使用 Fluent API 来配置关系是必需的还是可选的

    来源:Required and Optional Relationships

    【讨论】:

      【解决方案3】:

      最后似乎这是一个相当简单的答案,这个错误让我查看了我的类的结构并试图找出哪里出了问题。问题是在我的BranchContext(我没有考虑在我的问题中分享)中,我连接到OnModelCreating 并根据需要设置地址

      错误

      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity<EmployeeBranch>()
              .HasKey(s => new { s.BranchId, s.EmployeeId });
          modelBuilder.Entity<Branch>()
              .Property(s => s.Address).IsRequired();
          modelBuilder.Entity<Branch>()
              .Property(s => s.Name).IsRequired();
      
          base.OnModelCreating(modelBuilder);
      }
      

      正确

      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity<EmployeeBranch>()
              .HasKey(s => new { s.BranchId, s.EmployeeId });
      
          modelBuilder.Entity<Address>()
              .Property(s => s.AddressLine1).IsRequired();
          modelBuilder.Entity<Address>()
              .Property(s => s.Postcode1).IsRequired();
          modelBuilder.Entity<Address>()
              .Property(s => s.Postcode2).IsRequired();
          ...the other required elements...
          modelBuilder.Entity<Branch>()
              .Property(s => s.Name).IsRequired();
      
          base.OnModelCreating(modelBuilder);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-15
        • 2020-06-07
        • 1970-01-01
        • 1970-01-01
        • 2021-11-12
        • 2022-12-16
        • 1970-01-01
        • 2018-03-31
        相关资源
        最近更新 更多