且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我的表格是否正确链接到Entity Framework?

更新时间:2023-01-20 22:40:55

我也是在实体框架的开始,但是刺激我的是,你有一种外键关系, 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