如果不使用 TrackState 上的属性或为发布者 + 文章指定 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<TrackState>();
or
modelBuilder.Owned(typeof(TrackState));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)