c# - 模型生成错误

标签 c# entity-framework

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'.

但是它们都是整型的。所以他们应该匹配。我在这里缺少什么?

最佳答案

您在错误的属性上设置了属性 [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; }
  ...
}

关于c# - 模型生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39832214/

相关文章:

c# - JetBrains Rider C# | Windows 窗体用户界面

c# - 从服务器端以编程方式发送 Google Analytics 事件不起作用

entity-framework - Savechange 将一个索引替换为另一个索引

c# - 如何清理 Entity Framework 对象上下文?

c# - 如何将 XML 文件读取到 Dictionary<String,List<String>> 中,空字符串为 null 元素

c# - wpf强制构建可视化树

c# - ASP.NET mvc RenderAction 登录和注册 View

postgresql - NPGSQL 日期时间数组

entity-framework - 根据另一个字段的值增加 Entity Framework 中的字段

c# - Entity Framework ——通过名称获取实体