繁体   English   中英

实体框架无法识别外键?

[英]Entity Framework Not Recognizing Foreign Key?

我正在尝试像这样设置一个变量...

var userID = WebMatrix.WebData.WebSecurity.CurrentUserId;
var options = db.UserProfiles.Find(userID).TaskTypeSetting;

我正在使用基本Internet应用程序模板提供的用户身份验证表。

UserProfile中的行UserId与表TaskTypeSettingUser具有一对多关系

它告诉我它找不到TaskTypeSetting的定义。 如果我的人际关系设置正确,我是否应该能够做到这一点?

这是表格的模型:

namespace DailyTaskList.Models
{
    using System;
    using System.Collections.Generic;

    public partial class TaskTypeSetting
    {
        public int ID { get; set; }
        public int Type { get; set; }
        public int User { get; set; }
    }
}

好像没有在代码中添加关系? .edmx图显示了建立关系后的关系。

编辑:

UserProfile类的定义:

namespace DailyTaskList.Models
{
    using System;
    using System.Collections.Generic;

    public partial class UserProfile
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
    }
}

编译器错误消息: CS1061:'DailyTaskList.Models.UserProfile'不包含'TaskSetting'的定义,并且找不到扩展方法'TaskSetting'接受类型为'DailyTaskList.Models.UserProfile'的第一个参数(您是否缺少使用指令还是程序集引用?)

我也在“映射详细信息”下得到此信息:

Mappings are not allowed for an association over exposed foreign keys.

经过研究,我发现我遇到的是Visual Studio 2012和Entity Framework的错误。 实体框架无法正确生成我的代码。 这是由于.edmx文件嵌套在项目文件中引起的。

我使用的解决方案:

  • 右键单击.tt文件,然后选择“运行自定义工具”

其他解决方案:

  • 更新Visual Studio。
  • 将.edmx拖出项目文件夹。

我写了一篇关于这种情况的博客,可以在这里找到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM