本文介绍了如何使用FluentAPI添加等效的OwnedAttribute?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果不使用TrackState上的属性或为发布商+文章指定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来的人:如何使用流利的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<TrackState>();
或
modelBuilder.Owned(typeof(TrackState));
这篇关于如何使用FluentAPI添加等效的OwnedAttribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!