栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

什么是独立协会和外键协会?[重复]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

什么是独立协会和外键协会?[重复]

外键关联是除了对应的导航属性之外,模型中还具有外键属性的位置。独立关联是指您的数据库中有一个外键列,但与该列相对应的外键属性不在您的模型中-即,您具有NavigationProperty,但是没有外键属性可以告诉您该ID的ID值是多少。相关财产实际上并没有转到相关财产。

这是带有独立关联的模型的示例(请注意,从属没有外键-只是导航属性):

public class Dependent{    public int Id { get; set; }    [Required]    public Principal PrincipalEntity { get; set; }}public class Principal{    public int Id { get; set; }    public ICollection<Dependent> DependentEntities { get; set; }}public class MyContext : DbContext{    public DbSet<Dependent> Dependents { get; set; }    public DbSet<Principal> Principals { get; set; }}

这是具有外键关联的相同模型的示例(请注意PrincipalEntity_Id属性和[ForeignKey()]属性):

public class Dependent{    public int Id { get; set; }    public int PrincipalEntity_Id { get; set; }    [Required]    [ForeignKey("PrincipalEntity_Id")]    public Principal PrincipalEntity { get; set; }}public class Principal{    public int Id { get; set; }    public ICollection<Dependent> DependentEntities { get; set; }}public class MyContext : DbContext{    public DbSet<Dependent> Dependents { get; set; }    public DbSet<Principal> Principals { get; set; }}

请注意,您的数据库不会更改-基础数据库始终具有外键列,但是具有独立关联却未公开。

使用外键关联,您只需更改外键的值即可更新关系。如果您知道该值,则这很方便,因为您不需要加载要将导航属性更新到的实体。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433041.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号