导航属性“ UId”不是类型为“用户”的声明的属性

时间:2018-08-11 17:59:58

标签: c# entity-framework

因此,我已经尝试过在所有网站上找到的所有内容,但是我似乎找不到正确的答案。有人可以解决这个问题吗?

这是用户类:

[Table("Users")]
public class Users
{
    [Key,Required]
    public String UId { get; set; }
    public String Email { get; set; }
    public String Psw { get; set; }
    public Boolean Teacher { get; set;}



    public Users(string UId, string Email, string Psw, bool Teacher)
    {
        this.UId = UId;
        this.Email = Email;
        this.Psw = Psw;
        this.Teacher = Teacher;
    }

}

QAnswers类,考虑到它需要组合键,很可能是该问题:

[Table("Qanswers")]
public class QAnswers
{

    public int QId { get; set; }
    [ForeignKey("QId")]
    public Questions Questions { get; set; }

    public int AId { get; set; }
    [ForeignKey("AId")]
    public Answers Answers { get; set; }

    public String UId { get; set; }
    [ForeignKey("UId"), Column(Order =0)]
    public Users Users { get; set; }



    public QAnswers(int qId, int aId, string uId)
    {
        this.QId = qId;
        this.AId = aId;
        this.UId = uId;
    }
}

班级问题:

[Table("Questions")]
public class Questions
{
    [Key,Required]
    public int QId{ get; set; }
    public String Question { get; set; }
    public String Method { get; set; }
    public String Teacher { get; set; }



    public Questions(int qId, string question, string method, string teacher)
    {
        this.QId = qId;
        this.Question = question;
        this.Method = method;
        this.Teacher = teacher;
    }
}

和班级答案:

[Table("Answers")]
public class Answers
{
    [Key,Required]
    public int AId { get; set; }

    public int QId { get; set;}
    [ForeignKey("QId")]
    public virtual Questions Questions { get; set; }
    public String Answer { get; set; }



    public Answers(int aId, int qId, string answer)
    {
        this.AId = aId;
        this.QId = qId;
        this.Answer = answer;
    }
}

这是类WebsiteContext:

public class WebsiteContext : DbContext
{
    public WebsiteContext() : base("Database1")
    {

    }

    public DbSet<Users> Users{ get; set;}
    public DbSet<Questions> Questions { get; set; }
    public DbSet<Answers> Answers { get; set; }
    public DbSet<QAnswers> QAnswers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<QAnswers>().HasKey(x => new { x.QId, x.AId, 
        x.UId });
        //modelBuilder.Entity<Users>().HasKey(u => u.UId);
        modelBuilder.Entity<Users>().HasRequired(x => 
        x.UId).WithMany().WillCascadeOnDelete(false);
        //modelBuilder.Entity<Questions>().HasRequired(x => 
        x.QId).WithMany().WillCascadeOnDelete(false);
    }
}

这是完整的例外:

The navigation property 'UId' is not a declared property on type 'Users'. 
Verify that it has not been explicitly excluded from the model and that it 
is a valid navigation property.
Source=<Cannot evaluate the exception source>
StackTrace:<Cannot evaluate the exception stack trace>

好,所以问题显然是这样的:

modelBuilder.Entity<Users>().HasRequired(x => 
x.UId).WithMany().WillCascadeOnDelete(false);

但是,当我对此发表评论时,我会得到:

Introducing FOREIGN KEY constraint 'FK_dbo.Qanswers_dbo.Questions_QId' on 
table 'Qanswers' may cause cycles or multiple cascade paths. Specify ON 
DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY 
constraints.
Could not create constraint or index. See previous errors.
Source=.Net SqlClient Data Provider
StackTrace:<Cannot evaluate the exception stack trace>

那么,如果我不允许阻止级联,该如何解决呢?

1 个答案:

答案 0 :(得分:1)

关系由navigation properties定义。对于您的实体模型,它们是:

(1)Questions 1-> 0..N Answers

(2)Questions 1-> 0..N QAnswers

(3)Answers 1-> 0..N QAnswers

(4)Users 1-> 0..N QAnswers

所有都按->指示的方向进行级联删除。

您可以看到从QuestionsQAnswers多个级联路径

(A)Questions-> QAnswers
(B)Questions-> Answers-> QAnswers

涉及的关系是(1),(2),(3),因此必须至少为其中之一关闭级联删除。

表示每个关系的流利配置如下(请注意导航属性的使用位置,否则使用无参数重载):

(1)modelBuilder.Entity<Answers>().HasRequired(a => a.Questions).WithMany();

(2)modelBuilder.Entity<QAnswers>().HasRequired(a => a.Questions).WithMany();

(3)modelBuilder.Entity<QAnswers>().HasRequired(a => a.Answers).WithMany();

(4)modelBuilder.Entity<QAnswers>().HasRequired(a => a.Users).WithMany();

例如,要为所需的关系关闭级联删除,只需在其流畅的配置中添加.WillCascadeOnDelete(false)

modelBuilder.Entity<Answers>().HasRequired(a => a.Questions).WithMany()
    .WillCascadeOnDelete(false);