如何使用 FluentAPI 添加与 OwnedAttribute 等效的东西? [英] How do you add the equivalent of the OwnedAttribute using FluentAPI?

查看:13
本文介绍了如何使用 FluentAPI 添加与 OwnedAttribute 等效的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不使用 TrackState 上的属性或为 Publishers + 文章指定 OwnsOne,我似乎无法做到这一点.有什么方法可以在不使用属性的情况下将 TrackState 全局标记为拥有的类型?

I can't seem to do this without using either the attribute on TrackState or specifying OwnsOne for Publishers + Articles. Is there any way i can globally mark TrackState as an owned type without using the attribute?

(对于通过 google 来的人:如何使用 fluent api 向实体添加属性?)

(for people comming through google: How do you add attributes to entities using fluent api?)

(实体 + EF 核心在单独的库中,我不希望那里依赖 EF)

(Entities + EF core are in seperate libraries and i do not want a dependency there on EF)

public class Publisher
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Article> Articles { get; set; } = new List<Article>();

    public TrackState State { get; set; }
}

public class TrackState
{
    public DateTime? Created { get; set; }

    public DateTime? Modified { get; set; }
}

public class Article
{
    public int Id { get; set; }

    public int PublisherId { get; set; }

    public string Content { get; set; }

    public TrackState State { get; set; }
}


public class CustomContext : DbContext
{
    public CustomContext()
    {
    }

    public CustomContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet<Publisher> Publishers { get; set; }

    public DbSet<Article> Articles { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=(localdb)\mssqllocaldb;Database=BananaDb;Trusted_Connection=True;");
        //          base.OnConfiguring(optionsBuilder);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // one of the attempts i would have expected to work.
        modelBuilder.Entity<TrackState>().HasAnnotation("Owned", true);
        base.OnModelCreating(modelBuilder);
    }
}

推荐答案

您可以使用 ModelBuilder.Owned 方法重载:

You can use one of the ModelBuilder.Owned method overloads:

modelBuilder.Owned<TrackState>();

modelBuilder.Owned(typeof(TrackState));

这篇关于如何使用 FluentAPI 添加与 OwnedAttribute 等效的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆