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

时间:2019-05-03 22:14:30

标签: entity-framework ef-core-2.1

我真的是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:属性“ Branch.Address”的类型为“ Address”,当前数据库提供程序不支持。可以使用“ [NotMapped]”属性或通过“ OnModelCreating”中的“ EntityTypeBuilder.Ignore”来更改属性CLR类型或忽略该属性。

我创建了以下课程:

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

已创建以下内容:

DB structure

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

我该如何解决?最好不要更改结构,尽管我很高兴有足够的理由这样做

3 个答案:

答案 0 :(得分:6)

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

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

不幸的是,该错误还不够明显,但是暂时将错误的罪魁祸首注释掉将导致问题的根源。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

  

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

来源:Required and Optional Relationships

相关问题