EF Core中是否存在多个逆属性的命名约定或属性是否必要?

时间:2018-04-27 14:40:52

标签: c# entity-framework-core

VAE显示了下表示例:

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public string Description { get; set; }

    [ForeignKey("OnlineTeacher")]
    public int? OnlineTeacherId { get; set; }
    public Teacher OnlineTeacher { get; set; }

    [ForeignKey("ClassRoomTeacher")]
    public int? ClassRoomTeacherId { get; set; }
    public Teacher ClassRoomTeacher { get; set; }
}

public class Teacher
{
    public int TeacherId { get; set; }
    public string Name { get; set; }

    [InverseProperty("OnlineTeacher")]
    public ICollection<Course> OnlineCourses { get; set; }
    [InverseProperty("ClassRoomTeacher")]
    public ICollection<Course> ClassRoomCourses { get; set; }
}

我发现我可以删除[ForeignKey(*)]属性,但仍然可以在Courses表中正确创建它们。但是,如果我删除BOTH [InverseProperty]属性,则在构建时EF会抛出错误。是否有This article的命名约定?我的目标是密切关注命名约定并减少基于属性/流畅的配置量。

1 个答案:

答案 0 :(得分:0)

  

如果我删除BOTH [InverseProperty]属性,EF会抛出错误   当我建立。是否有这样的命名约定   外键?

当两个相同类型的许多关联(例如1对多)在两个实体之间退出时,EF定义了无命名约定

例如,当const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const CleanWebpackPlugin = require('clean-webpack-plugin'); const webpack = require('webpack'); module.exports = { entry: { app: './src/index.js' }, devServer: { // contentBase: './dist', contentBase: __dirname + "/assets/", hot: true, }, plugins: [ new CleanWebpackPlugin(['dist']), new HtmlWebpackPlugin({ title: 'Output Management' }), new webpack.NamedModulesPlugin(), new webpack.HotModuleReplacementPlugin() ], output: { path: __dirname + "/assets/", filename: 'bundle.js', chunkFilename: '[name].js' }, }; EntityA之间存在一个关联时。假设它在EntityBEntityA之间存在1对*的关系,当您尊重约定时,它会很有效。但是当你在EntityBEntityA之间建立另一个1对*的关系时,EF需要你的帮助才能知道关联是如何相关的。因此,您必须使用EntityB数据注释属性或通过流畅配置定义完整关系。