zoukankan      html  css  js  c++  java
  • 关于EntityFramework 4.1 中Code First方式的一些记录

    1、在一对多关系中,我们需要指定一个外键,同时我们使用virtual 这个标识标识另外一个对象的引用

    比如说以下模型

    public class Phone
    {
        public int Id{get;set;}
        public int PhoneBrandId{get;set;}
        public virtual PhoneBrand PhoneBrand{get;set;}
    }
    public class PhoneBrand{
        public int Id{get;set;}
        public string BrandName{get;set;}
        public ICollection<Phone> Phones
    }
     
    注意,在具体的想访问Phone对象中的PhoneBrand 之前,这个PhoneBrand 都是null 的,只有PhoneBrandId 是有值的
     
    2、关于在两个对象中有重复引用的情况下的处理
    比如说我们可能遇见这种情况,我们需要保存一篇文章的发表者和修改者
    比如这里
    public class Post
        {
            public int Id { get; set; }
            public string Title { get; set; }
            public DateTime DateCreated { get; set; }
            public string Content { get; set; }
            public int BlogId { get; set; }
            public Blog Blog { get; set; }
            public ICollection<Comment> Comments { get; set; }
            //此处是个典型,两个都引用了Person这个对象,但是请注意,这里并没有直接有id号
             public Person CreatedBy { get; set; }
            public Person UpdatedBy { get; set; }
        }
     
     
    我们在做Person这个对象的时候,就需要使用[InverseProperty(对应的字段)]标记一下
    public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            [InverseProperty("CreatedBy")]
            public List<Post> PostsWritten { get; set; }
            [InverseProperty("UpdatedBy")]
            public List<Post> PostsUpdated { get; set; }
        }
  • 相关阅读:
    hdu 1166 敌兵布阵
    linux(debian)系统django配远程连接sqlserver数据库
    [机器学习] Coursera ML笔记
    UICollectionView使用方法补充(照片轮播墙)
    word中公式的排版及标题列表
    FZOJ2110: Star
    Windows 下的 Makefile 编写
    掌握VS2010调试 -- 入门指南
    Visual Studio 2010初学者的调试指南:Mastering Debugging in Visual Studio 2010
    AlphaGo:用机器学习技术古老的围棋游戏掌握AlphaGo: Mastering the ancient game of Go with Machine Learning
  • 原文地址:https://www.cnblogs.com/jicheng1014/p/2051191.html
Copyright © 2011-2022 走看看