我的表格是否正确链接到Entity Framework? [英] Are my tables linked correctly for Entity Framework?
问题描述
当我尝试查询数据库时,我遇到了一个问题。这是我使用的查询:
int? UserId = db.StudentModel
.Where(c => c.UserName == certUserName)
.Select(c => c.UserId)
.FirstOrDefault();
当它搜索数据库时,它成功检索UserId。
问题是我使用以下查询:
CompletionsModel student = db.Completions.Find(UserId);
当我这样做时,它会抛出一个内部异常,指出
{列名无效UserProfile_UserId'。}
是当我将代码和鼠标放在命令的db部分,看看它持有的数据,它有CourseModel,StudentModel和Completions(虽然该模型的实际文件名是CompletionsModel - 这是一个线索吗?),所以它似乎像他们正确连接。
这是我的三个模型和数据库上下文的代码。
CompletionsModel(UserProfile是我的代码中的白色文本;不知道为什么这里的teal在这里与UserId和CompletionDate相同):
[Table(Completion)]
public class CompletionsModel
{
[Key]
public int UserId {get;组; }
public string PRD_NUM {get;组; }
public DateTime CompletionDate {get;组; }
public virtual CourseModel PRD {get;组; }
public virtual StudentModel UserProfile {get;组; }
}
CourseModel:
[Table(PRD)]
public class CourseModel
{
[Key]
public string PRD_NUM {get;组; }
public string PRD_TIT {get;组; }
//因为任何数量的学生可以注册一个课程
public virtual ICollection< CompletionsModel> CompletionsModel {get;组; }
}
StudentModel:
[Table(UserProfile)]
public class StudentModel
{
[Key]
public int UserId {get;组; }
public string UserName {get;组; }
public virtual ICollection< CompletionsModel> CompletionsModel {get;组; }
}
DBContext:
public class ClassContext:DbContext
{
public ClassContext()
:base(DefaultConnection)
{
}
public DbSet< StudentModel> StudentModel {get;组; }
public DbSet< CompletionsModel>达成数{get;组; }
public DbSet< CourseModel> CourseModel {get;组; }
}
最后,我的数据库布局的图像 - 也许这将有助于事情同时也是:
我也是在实体框架的开始,但是刺激我的是,你有一种外键关系, em>完成和 UserProfile ,没有真正定义,您的完成表中的int列作为外键。
您可以尝试更改
public virtual StudentModel UserProfile {get;组;
像
public virtual int StudentId {get;组; }
[ForeignKey(StudentId)]
public virtual StudentModel UserProfile {get;组;
与 PRD相同
但是可能您的数据库中的更改不是您想要的。
你还可以做的是删除这些行
public virtual CourseModel PRD {get;组; }
public virtual StudentModel UserProfile {get;组;
希望有所帮助。
编辑:
我猜问题是,你的完成中缺少 ForeignKey
属性在您的 UserProfile 属性中, em>表。
所以使用
[ForeignKey(UserId)]
public virtual StudentModel UserProfile {get;组;
而不是
public virtual StudentModel UserProfile {get;组;
如果UserIds表示相同的用户
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();
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
{"Invalid column name 'UserProfile_UserId'."}
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.
Here's the code for my three models and the database context.
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:
[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:
[Table("UserProfile")]
public class StudentModel
{
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<CompletionsModel> CompletionsModel { get; set; }
}
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:
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.
You could try to change
public virtual StudentModel UserProfile { get; set; }
to something like
public virtual int StudentId { get; set; }
[ForeignKey("StudentId")]
public virtual StudentModel UserProfile { get; set; }
The same for 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; }
Hope that helps.
EDIT:
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; }
instead of
public virtual StudentModel UserProfile { get; set; }
if the UserIds are representing the same user
这篇关于我的表格是否正确链接到Entity Framework?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!