如果不使用 TrackState 上的属性或为 Publishers + Articles 指定 OwnsOne,我似乎无法做到这一点。有什么方法可以在不使用属性的情况下将 TrackState 全局标记为拥有类型?
(对于通过 google 访问的人:如何使用 fluent api 向实体添加属性?)
(实体 + EF 核心在单独的库中,我不希望那里依赖于 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 之一方法重载:
modelBuilder.Owned<TrackState>();
或
modelBuilder.Owned(typeof(TrackState));
关于c# - 如何使用 Fluent API 添加等效的所有者属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53730130/