Dbl*_*Dbl 4 c# entity-framework-core ef-core-2.1
如果不使用 TrackState 上的属性或为 Publishers + 文章指定 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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用ModelBuilder.Owned方法重载之一:
modelBuilder.Owned<TrackState>();
Run Code Online (Sandbox Code Playgroud)
或者
modelBuilder.Owned(typeof(TrackState));
Run Code Online (Sandbox Code Playgroud)