The property X is of type Y which is not supported by current database provider

后端 未结 3 1453
-上瘾入骨i
-上瘾入骨i 2020-12-20 13:18

I am really new to EF (using EF core 2.1) and have been following a bunch of tutorials so far, but now I have ventred in to creating my own DB structure and stumbled when tr

相关标签:
3条回答
  • 2020-12-20 13:53

    It seems it was a fairly simple answer in the end, the error had me looking at the structure of my classes and trying to work out what had gone wrong. The problem was that in my BranchContext (which I did not consider sharing in my question), I hooked into OnModelCreating and set Address as required

    Wrong

    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);
    }
    

    Right

    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);
    }
    
    0 讨论(0)
  • 2020-12-20 13:54

    Assumption: the goal is to make Address a required property of Branch

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

    You can use the Fluent API to configure whether the relationship is required or optional

    Source: Required and Optional Relationships

    0 讨论(0)
  • 2020-12-20 14:06

    This error might also pop up in silly situations of referencing a navigation property instead of a mapped property within a constraint or similar:

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

    Unfortunately, the error is not explicit enough, but temporarily commenting out the culprit from the error will lead to the source.

    0 讨论(0)
提交回复
热议问题