小编Ken*_*man的帖子

在EF 6中找不到HasOne

我是Entity Framework的新手,我正试图找出关系.我找到了这段代码:

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; …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-fluent-api

6
推荐指数
1
解决办法
5674
查看次数

标签 统计

c# ×1

ef-fluent-api ×1

entity-framework ×1