官术网_书友最值得收藏!

Post entity

Create a Post.cs class file and include the following properties:

    public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime PublishedDateTime { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}

The BlogId property is a foreign key created for the corresponding Blog navigation property. As you may notice in this case, we have an inpidual item as the navigation property, as opposed to a list in the Blog entity. This is where relationship type comes into the picture, which we will be exploring more in Chapter 3, Relationships – Terminology and Conventions.

EF will allow us to create an inpidual navigation property without any foreign key in the entity. In those cases, EF will create a foreign key for us in the database table using the BlogId pattern (the Blog navigation property along with its  Id primary key). EF will generate them automatically for all navigational properties against the Id primary key, but it also allows us to name it differently and decorate it via a custom attribute.

We have built the schema required for the application so far, but it was not configured in EF, so let's see how the data models get connected/configured with EF using database context.

主站蜘蛛池模板: 迭部县| 会宁县| 罗田县| 皋兰县| 保定市| 视频| 晋江市| 义乌市| 峨边| 永胜县| 宁陵县| 平潭县| 南宫市| 卫辉市| 乌兰察布市| 苏尼特右旗| 营山县| 祁阳县| 石嘴山市| 栖霞市| 元阳县| 天祝| 崇明县| 电白县| 瑞丽市| 紫阳县| 广灵县| 泰顺县| 盐津县| 青铜峡市| 黔南| 繁峙县| 盐边县| 措勤县| 鹤庆县| 抚州市| 中牟县| 九江市| 泰和县| 宿迁市| 马公市|