https://www.learnentityframeworkcore.com/configuration/fluent-api/model-configuration
https://www.learnentityframeworkcore.com/configuration/fluent-api/ignore-method數據庫
ef core 默認建立 table使用的schema是 dbo, 能夠經過下面的方式修改schema:api
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("MyCustomSchema"); }
固然你不是想把全部的entity都map到數據庫中的表, 可使用Ignore<TEntity>()
的方式:app
public class SampleContext : DbContext { public DbSet<Contact> Contacts { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Ignore<AuditLog>(); modelBuilder.Entity<Contact>().Ignore(c => c.FullName); } } public class Contact { public int Id { get; set; } public string FullName { get; set; } public string Email { get; set; } public AuditLog AuditLog { get; set; } public string FullName => $"{FirstName} {LastName}"; } public class AuditLog { public int EntityId { get; set; } public int UserId { get; set; } public DateTime Modified { get; set; } }
類似的convention的方式:ide
public class Contact { public int ContactId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public AuditLog AuditLog { get; set; } [NotMapped] public string FullName => $"{FirstName} {LastName}"; } [NotMapped] public class AuditLog { public int EntityId { get; set; } public int UserId { get; set; } public DateTime Modified { get; set; } }