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; }
        }
  • 相关阅读:
    Redis Cluster 剔除节点失败
    redis cluster 常用操作
    pika版本特性研究
    ueditor的集成
    pyhon类
    python之eval简述
    Python:list,tuple
    Python函数式编程学习:lambda, map, reduce, filter、sorted()、lambda、decorator
    Python中字典详解
    Python调用(运行)外部程序
  • 原文地址:https://www.cnblogs.com/jicheng1014/p/2051191.html
Copyright © 2011-2022 走看看