是我的表中正确关联的实体框架? [英] Are my tables linked correctly for Entity Framework?

查看:130
本文介绍了是我的表中正确关联的实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预警:我大概知道什么时候还的MVC /实体框架/ LINQ查询

Forewarning: I know approximately nothing when it comes to MVC/Entity Framework/Linq queries.

我有一个问题,当我尝试查询数据库。下面是我使用的查询:

I'm having an issue when I try to query the database. Here's the query I'm using:

int? UserId = db.StudentModel
    .Where(c => c.UserName == certUserName)
    .Select(c => c.UserId)
    .FirstOrDefault();

在其搜索数据库,它成功地获取用户ID。

When it searches the database, it successfully retrieves the UserId.

问题是,我可以使用下面的查询:

The problem is that I then use the following query:

CompletionsModel student = db.Completions.Find(UserId);

当我这样做,它引发了美国内部异常

When I do this, it throws an inner exception that states

{无效的列名称UserProfile_UserId'。}

{"Invalid column name 'UserProfile_UserId'."}

奇怪的是,当我去我的code和鼠标命令的DB部分看到它的持有什么样的数据,它有CourseModel,StudentModel,完井(虽然模型的的实际的文件名是CompletionsModel - 是一个线索),所以它的看起来的像他们正确链接

The weird thing is that when I go to my code and mouse over the 'db' part of the command to see what data it's holding, it has CourseModel, StudentModel, and Completions (though the model's actual filename is CompletionsModel - is that a clue?), so it seems like they're linked properly.

这里的code为我的三个模型和数据库环境。

Here's the code for my three models and the database context.

CompletionsModel(用户配置在我的code白字,不知道为什么它水鸭在这里用相同用户名和CompletionDate):

CompletionsModel (UserProfile is white text in my code; not sure why it's teal here Same with UserId and CompletionDate):

[Table("Completion")]
    public class CompletionsModel
    {
        [Key]
        public int UserId { get; set; }
        public string PRD_NUM { get; set; }
        public DateTime CompletionDate { get; set; }

        public virtual CourseModel PRD { get; set; }
        public virtual StudentModel UserProfile { get; set; }
    }

CourseModel:

CourseModel:

[Table("PRD")]
    public class CourseModel
    {
        [Key]
        public string PRD_NUM { get; set; }
        public string PRD_TIT { get; set; }

        //because any number of students can be enrolled in one course
        public virtual ICollection<CompletionsModel> CompletionsModel { get; set; }
    }

StudentModel:

StudentModel:

[Table("UserProfile")]
    public class StudentModel
    {
        [Key]
        public int UserId { get; set; }
        public string UserName { get; set; }

        public virtual ICollection<CompletionsModel> CompletionsModel { get; set; }
    }

的DbContext:

DBContext:

public class ClassContext : DbContext
{
    public ClassContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<StudentModel> StudentModel { get; set; }
    public DbSet<CompletionsModel> Completions { get; set; }
    public DbSet<CourseModel> CourseModel { get; set; }
}

最后,我的数据库布局的图像 - 也许这将顺水推舟,也:

And finally, an image of my database layout - maybe this will help things along, too:

推荐答案

我太在实体框架的开始,但到底是什么刺激我的是,你必须的完成用户配置的,没有真正定义,一个INT列作为外键的表完成

I'm too at the beginning of Entity Framework, but what does irritate me is, that you have kind of foreign key relationship between Completion and UserProfile, without really defining, a int column as foreign key in your Completion Table.

您可以尝试更改

public virtual StudentModel UserProfile { get; set; }

要像

public virtual int StudentId { get; set; }
[ForeignKey("StudentId")]
public virtual StudentModel UserProfile { get; set; }

同为 PRD

但是,也许在你的数据库中的变化是不是你想要的。
你也可以做的,是要删除这些行

But maybe a change in your database is not what you want. What you could also do, is to remove these lines

public virtual CourseModel PRD { get; set; }
public virtual StudentModel UserProfile { get; set; }

希望有所帮助。

编辑:
我想这个问题是,你缺少你的的完成的表在你的用户配置的属性 ForeignKey的属性。
因此,使用

I guess the problem is, that you are missing the ForeignKey Attribute at your UserProfile property in your Completions table. So use

[ForeignKey("UserId")]
public virtual StudentModel UserProfile { get; set; }

而不是

public virtual StudentModel UserProfile { get; set; }

如果该用户ID重新presenting相同的用户

if the UserIds are representing the same user

这篇关于是我的表中正确关联的实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆