zoukankan      html  css  js  c++  java
  • EF多个表映射

    public class Media // One entity table
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool Enabled { get; set; }
    
        public virtual ICollection<ContractMedia> ContractMedias { get; set; }
    }
    
    public class Contract // Second entity table
    {
        public int Id { get; set; }
        public string Code { get; set }
    
        public virtual ICollection<ContractMedia> ContractMedias { get; set; }
    }
    
    public class ContractMedia // Association table implemented as entity
    {
        public int MediaId { get; set; }
        public int ContractId { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public double Price { get; set; }
    
        public virtual ICollection<Media> Medias { get; set; }
        public virtual ICollection<Contract> Contracts { get; set; }
    }
    

    And after you created models/entities, you need to define relationships in context:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       modelBuilder.Entity<ContractMedia>()
           .HasKey(c => new { c.MediaId, c.ContractId });
    
       modelBuilder.Entity<Contract>()
           .HasMany(c => c.ContractMedias)
           .WithRequired()
           .HasForeignKey(c => c.ContractId);
    
       modelBuilder.Entity<Media>()
           .HasMany(c => c.ContractMedias)
           .WithRequired()
           .HasForeignKey(c => c.MediaId);  
    }
  • 相关阅读:
    Manacher-模版题poj3974 hdu3068
    拓展kmp(带注释版)
    颓の第17周
    php 递归遍历目录带缩进
    php 递归遍历目录
    php session
    apache主机配置
    php环境配置的检测方法
    php 变量
    php MVC
  • 原文地址:https://www.cnblogs.com/panjuan/p/4800335.html
Copyright © 2011-2022 走看看