我有一个复合类型,我通过调用 OwnsOne 通过 Fluent API 进行配置。
因此,将根据模板 {Property name}_{Field name} 在数据库中创建字段。
有什么办法可以覆盖这种行为?
这是一个 metanit 的例子:
using Microsoft.EntityFrameworkCore;
namespace HelloApp
{
public class User
{
public int Id { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public UserProfile Profile { get; set; }
}
public class UserProfile
{
public string Name { get; set; }
public int Age { get; set; }
}
public class ApplicationContext : DbContext
{
public DbSet<User> Users { get; set; }
public ApplicationContext()
{
Database.EnsureDeleted();
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=relationsdb;Trusted_Connection=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().OwnsOne(u => u.Profile);
}
}
}
像这样的东西:
结果: