2016-10-03 89 views
1

Riddle_Question: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'RiddleId' on entity 'Question' does not match the type of property 'Id' on entity 'Riddle' in the referential constraint 'Question_Riddle'.模型生成錯誤

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

Riddle_Question: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'RiddleId' on entity 'Question' does not match the type of property 'Id' on entity 'Riddle' in the referential constraint 'Question_Riddle'.

Source Error:

Line 76:    // This doesn't count login failures towards account lockout 
Line 77:    // To enable password failures to trigger account lockout, change to shouldLockout: true 
Line 78:    var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false); 
Line 79:    switch (result) 
Line 80:    { 

裏德爾型號:

public class Riddle 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    [MaxLength(200)] 
    [DataType(DataType.MultilineText)] 
    public string Description { get; set; } 
    public virtual List<Review> Reviews { get; set; } 
    public virtual ApplicationUser User { get; set; } 
    public virtual List<Question> Questions { get; set; } 
    [Column(TypeName = "datetime2")] 
    public DateTime CreationDate { get; set; } 
} 

問題型號:

public class Question 
    { 
     public int Id { get; set; } 
     [MaxLength(2000)] 
     [DataType(DataType.MultilineText)] 
     public string Body { get; set; } 
     public string Answer { get; set; } 
     public Riddle Riddle { get; set; } 
     [Column(TypeName = "datetime2")] 
     public int RiddleId { get; set; } 
     public DateTime CreationDate { get; set; } 
     public int QuestionNumber { get; set; } 
    } 

它說

The type of property 'RiddleId' on entity 'Question' does not match the type of property 'Id' on entity 'Riddle' in the referential constraint 'Question_Riddle'.

但他們都在噸。所以他們應該匹配。我在這裏錯過了什麼?

回答

2

您對屬性的屬性爲[Column(TypeName = "datetime2")]。它應該是超過了CreationDate,而不是RiddleId

public class Question { 
    ... 
    public Riddle Riddle { get; set; } 
    public int RiddleId { get; set; } 
    [Column(TypeName = "datetime2")] 
    public DateTime CreationDate { get; set; } 
    ... 
} 
+0

大聲笑吧。我怎麼看不到?謝謝伊戈爾。 – Gimballock

1

好像它只是一個錯字抱着你back.You被指定的RiddleId場應與列屬性類型DATETIME2的。

[Column(TypeName = "datetime2")] 
public int RiddleId { get; set; } 

也許應該是 -

public int RiddleId { get; set; } 
[Column(TypeName = "datetime2")] 
public DateTime CreationDate { get; set; }