属性 X 是当前数据库提供程序不支持的类型 Y

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

我真的是 EF 的新手(使用 EF core 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: The property 'Branch.Address' is of type 'Address' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

我创建了以下 类:

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

创建了以下内容:

要明确这是一个运行时错误,我已经看到几个线程在尝试更新数据库时遇到此错误 here and 但他们的讨论没有给我指明正确的方向(也许我遗漏了显而易见的东西)。

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

这似乎是一个相当简单的答案,错误让我查看了我的 类 的结构并试图找出哪里出了问题。问题是在我的 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);
}

假设:目标是使 Address 成为 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

来源:Required and Optional Relationships

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

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

不幸的是,错误不够明确,但暂时从错误中注释掉罪魁祸首将导致源头。