我是Entity Framework代码优先的新手。这是我在ASP.NET MVC中的学习,使用代码优先的数据库创建。
我有两节课:
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Standard { get; set; }
public int SubjectId { get; set; }
[ForeignKey("SubjectId")]
public ICollection<Subject> Subjects { get; set; }
}
public class Subject
{
[Key]
public int SubjectId{ get; set; }
public string SubjectName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试向表中插入一条Student记录Student,该记录具有SubjectId引用该Subject表的外键。
我正在尝试两种可能的方法:
第一种方法
using(var cxt = new SchoolContext()) …Run Code Online (Sandbox Code Playgroud) 这里我们在EF Core上有3个表:
还有更多(新闻,项目除外):内容,帖子,表格等
和我的模型定义
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public Link Link { get; set; }
}
public class News
{
public int Id { get; set; }
public string Header { get; set; }
public string Content { get; set; }
public Link Link { get; set; }
}
public class Link
{
public int Id …Run Code Online (Sandbox Code Playgroud) c# entity-framework-core .net-core asp.net-core ef-code-first-mapping
我有 2 个类(实体)Exemption 和 Period,我想使用来自豁免的 IdNumber 和来自 Period 的 Id 创建一个唯一索引
public class Exemption
{
[Key]
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
.
.
.
[Required]
public string IdNumber { get; set; }
[Required]
public Period Period { get; set; }
}
public class Period
{
[Key]
public int Id { get; set; }
.
.
.
[Required]
public string Year { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用 Fluent API 我在OnModelCreating …
.net entity-framework ef-code-first asp.net-core ef-code-first-mapping
我正在尝试为我的Ticket班级搭建一个剃刀页面集,当实体框架尝试创建数据库时出现错误:
我的理解是,如果您有一个属性“Id”,它将使用它,并且如果您设置 [Key] 注释,它将专门告诉 Entity Framework 使用该属性。这两样东西我都有,那怎么办?
c# entity-framework-core ef-code-first-mapping entity-framework-migrations asp.net-core-scaffolding