zoukankan      html  css  js  c++  java
  • Entity Framework Core Relationship的学习笔记

    说明

    此例筛选了感兴趣及常用部分

    参考文献

    https://docs.microsoft.com/en-us/ef/core/modeling/relationships

    One to Many

    Many to Many

    新增一个中间类,再转换成One to Many及One to Many的形式

    class MyContext : DbContext
    {
        public DbSet<Post> Posts { get; set; }
        public DbSet<Tag> Tags { get; set; }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PostTag>()
                .HasKey(t => new { t.PostId, t.TagId });
    
            modelBuilder.Entity<PostTag>()
                .HasOne(pt => pt.Post)
                .WithMany(p => p.PostTags)
                .HasForeignKey(pt => pt.PostId);
    
            modelBuilder.Entity<PostTag>()
                .HasOne(pt => pt.Tag)
                .WithMany(t => t.PostTags)
                .HasForeignKey(pt => pt.TagId);
        }
    }
    
    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
    
        public List<PostTag> PostTags { get; set; }
    }
    
    public class Tag
    {
        public string TagId { get; set; }
    
        public List<PostTag> PostTags { get; set; }
    }
    
    public class PostTag
    {
        public int PostId { get; set; }
        public Post Post { get; set; }
    
        public string TagId { get; set; }
        public Tag Tag { get; set; }
    }

    One to One(One to Zero)

    class MyContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<BlogImage> BlogImages { get; set; }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>()
                .HasOne(p => p.BlogImage)
                .WithOne(i => i.Blog)
                .HasForeignKey<BlogImage>(b => b.BlogForeignKey);
        }
    }
    
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    
        public BlogImage BlogImage { get; set; }
    }
    
    public class BlogImage
    {
        public int BlogImageId { get; set; }
        public byte[] Image { get; set; }
        public string Caption { get; set; }
    
        public int BlogForeignKey { get; set; }
        public Blog Blog { get; set; }
    }

    使用Migraion

    将Model模型放在单独的类库中

    使用CLI commands

    需要在此类库中安装Migration必须的Nuget包,编辑.csproj 

    <ItemGroup>
      <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
      <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
    </ItemGroup>

    MSSql Server

    dotnet ef migrations add Initial -c SchoolContext -o Data/SqlServerMigrations -s ../RelationshipStudy
    
    dotnet ef database update -s ../RelationshipStudy
    
    dotnet ef migrations remove -c SchoolContext -s ../RelationshipStudy
  • 相关阅读:
    048——VUE中使用animate.css动画库控制vue.js过渡效果
    047——VUE中css过渡动作实例
    046——VUE中组件之使用动态组件灵活设置页面布局
    045——VUE中组件之父组件使用scope定义子组件模板结构
    004PHP文件处理——目录操作:glob rewinddir opendir readdir
    003PHP文件处理——目录操作:rename scandir
    044——VUE中组件之使用内容分发slot构建bootstrap面板panel
    Linux输出信息并将信息记录到文件(tee命令)
    linux下使用SVN上传项目
    linux下将文件上传到svn服务器
  • 原文地址:https://www.cnblogs.com/hahaxi/p/7705236.html
Copyright © 2011-2022 走看看