EF Core 2.1'UNIQUE约束失败:People.ReferredById'

时间:2018-08-02 04:07:07

标签: entity-framework asp.net-core entity-framework-core ef-core-2.0

我的数据模型

Table("People")]
public abstract class Person
{
    public long Id { get; set; }

    public string Name { get; set; }

    [ForeignKey("ReferredBy")]
    public long? ReferredById { get; set; }
    public User ReferredBy { get; set; }
}


public class User : Person
{
    public string UserName { get; set; }
}

public class Student : Person
{
    public string RollNo { get; set; }

}

Fluent API在删除时设置为空

        modelBuilder.Entity<Person>()
            .HasOne(t => t.ReferredBy)
            .WithOne()
            .OnDelete(DeleteBehavior.SetNull);

现在,当我尝试插入具有相同ReferredByit的行时,将引发唯一约束错误。请帮助我。

0 个答案:

没有答案