我们已经知道了,Code--First默认的约定,如果你没有包含外键属性在父类中,那么他会为我们创建{Class Name}_{primary Key}外键。这个InverseProperty特性用在:类之间当有多重关系的时候。

看下下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF3
{
   public class Student
    {
       public int StudentId { get; set; }

       public string StudentName { get; set; }

       public Standard CurrentStandard { get; set; }

       public Standard PriviousStandard { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EF3
{
   public class Standard
    {
       public int StandardId { get; set; }

       public string StandardName { get; set; }

       public ICollection<Student> CurrentStudent { get; set; }

       public ICollection<Student> PreviousStudent { get; set; }

    }
}

  上面的代码中,Student实体包含两个Standard类型的导航属性,同样的Standard实体包含两个集合类型的Student导航属性,Code-First创建了4个列为他们之间的关系:

InverseProperty这个特性可以重写这个默认的约定,下面的代码中,我们可以在Standard中使用InverseProperty特性来修正这个问题。

我们先看一下错误的例子:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;

namespace EF3
{
   public class Standard
    {
       public int StandardId { get; set; }

       public string StandardName { get; set; }

       /// <summary>
       /// InverseProperty,反属性特性
       /// </summary>
       [InverseProperty("one")]
       public ICollection<Student> CurrentStudent { get; set; }

       /// <summary>
       /// InverseProperty,反属性特性
       /// </summary>
       [InverseProperty("two")]
       public ICollection<Student> PreviousStudent { get; set; }

    }
}

上面的反属性特性里面我随便输入不存在的字符,然后:

提示这个反属性特性作用的属性CurrnentStudent不合法,这个属性one不是合法的导航属性。

然后我们看下正确的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;

namespace EF3
{
   public class Standard
    {
       public int StandardId { get; set; }

       public string StandardName { get; set; }

       /// <summary>
       /// InverseProperty,反属性特性
       /// </summary>
       [InverseProperty("CurrentStandard")]
       public ICollection<Student> CurrentStudent { get; set; }

       /// <summary>
       /// InverseProperty,反属性特性
       /// </summary>
       [InverseProperty("PriviousStandard")]
       public ICollection<Student> PreviousStudent { get; set; }

    }
}

这个代码里面把Student实体中的导航属性的名称放进去。就可以了。

你当然可以使用外键特性来包含外键属性,请看下面的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF3
{
   public class Student
    {
       public int StudentId { get; set; }

       public string StudentName { get; set; }

       public int ForeignKeyCurrent { get; set; }
       public int ForeignKeyPrevious { get; set; }

       [ForeignKey("ForeignKeyCurrent")]
       public Standard CurrentStandard { get; set; }

        [ForeignKey("ForeignKeyPrevious")]
       public Standard PriviousStandard { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;

namespace EF3
{
   public class Standard
    {
       public int StandardId { get; set; }

       public string StandardName { get; set; }

       /// <summary>
       /// InverseProperty,反属性特性
       /// </summary>
       [InverseProperty("CurrentStandard")]
       public ICollection<Student> CurrentStudent { get; set; }

       /// <summary>
       /// InverseProperty,反属性特性
       /// </summary>
       [InverseProperty("PriviousStandard")]
       public ICollection<Student> PreviousStudent { get; set; }

    }
}

等等,你以为就样就可以了呢?我们运行程序看看就知道了:

果不其然又出错了,看一下具体的信息:

将 FOREIGN KEY 约束 'FK_dbo.Students_dbo.Standards_ForeignKeyPrevious' 引入表 'Students' 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
无法创建约束。请参阅前面的错误消息。

百度了一下:http://www.cnblogs.com/chear/archive/2012/11/09/2762145.html

改一下我们的上下文类的代码:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF3
{
   public class DbContextClass:DbContext
    {
       public DbContextClass():base("ConStr") { }

     public  DbSet<Student> Studnets { get; set; }

     public DbSet<Standard> Standards { get; set; }

     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
         Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>());
//加上这句代码,就OK了,取消级联删除。(这里其实可以两句代码,只写一句也是可以的。亲测过!!)
         modelBuilder.Entity<Standard>().HasMany(t => t.CurrentStudent).WithRequired(p=>p.CurrentStandard).WillCascadeOnDelete(false);

modelBuilder.Entity<Standard>().HasMany(t => t.PreviousStudents).WithRequired(p => p.PriviousStandard).WillCascadeOnDelete(false);

base.OnModelCreating(modelBuilder);
     }
    }
}

然后再运行程序就可以了。

Thus, you can use InverseProperty and ForeignKey attribute for multiple relationships between the same classes.

所以,当有多重关系的时候,你可以使用InverseProperty特性和外键特性。

当我们运行的时候,保存报错,提示主外键冲突什么的,那就是主键表Standard里面没有数据,我们插入两条数据进去,就可以了。

运行之后;

7.11 数据注解特性--InverseProperty的更多相关文章

  1. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...

  2. 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code- ...

  3. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...

  4. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  5. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...

  6. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  7. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

  8. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

    原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...

  9. 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...

随机推荐

  1. linux 中部署ant编译的包中缺少问题

    今天遇到在window上部署ant编译的包,能运行正常,但部署在linux中出现跳不进jsp中,出现404问题,后来经过排查在jsp中<%@taglib prefix="c" ...

  2. 初识Python

    Python 简介 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有 ...

  3. Concurrency并发性

    今天看了有关性能的文章,性能也是客户所看重的. 文章推荐看了软件编程并发性. 就按书上敲了网址看:http://www.gotw.ca/publications/concurrency-ddj.htm ...

  4. [转]ios push

    转:http://blog.csdn.net/showhilllee/article/details/8631734 APNS的推送机制 首先我们看一下苹果官方给出的对ios推送机制的解释.如下图 P ...

  5. 探索c#之跳跃表(SkipList)

    阅读目录: 基本介绍 算法思想 演化步骤 实现细节 总结 基本介绍 SkipList是William Pugh在1990年提出的,它是一种可替代平衡树的数据结构. SkipList在实现上相对比较简单 ...

  6. com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK assets/com.xx.xx

    完整的Error 信息(关键部分) Error:Execution failed for task ':fanwe_o2o_47_mgxz_dingzhi:transformResourcesWith ...

  7. 使用Ldoc给Lua生成文档

    Ldoc介绍 LDoc是一个Lua的文档生成工具,过去,比较常用的Lua生成文档的工具是LuaDoc,可惜作者自从2008年之后就再也没有发布过新的版本了,说明作者基本上已经放弃维护了.而LDoc则是 ...

  8. 彻底理解nth-child和nth-of-type的区别。

    最近又有些天没写博客了,主要写一篇下来,太浪费时间了,其实这不是根本,根本是最近比较忙,忙什么呢?最近发现一个问题觉得学习速度太慢了,时间倒是花的很多,但大部分时间都花在无意义的事情上,所有打算改变政 ...

  9. SQL Server 跨网段(跨机房)复制

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搭建过程(Process) 注意事项(Attention) 参考 ...

  10. Html 特殊符号

    HTML特殊符号对照表 特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 Α Α Α Β Β Β Γ Γ Γ Δ Δ Δ Ε Ε Ε Ζ Ζ Ζ Η Η Η Θ Θ Θ Ι Ι Ι Κ ...