[entity framework core] Entity Framework Core Model Configuration

1917 ワード

https://www.learnentityframeworkcore.com/configuration/fluent-api/model-configuration https://www.learnentityframeworkcore.com/configuration/fluent-api/ignore-method Schema ef coreのデフォルトでtableを作成するために使用されるschemaはdboです.schema:
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("MyCustomSchema");
    }
exclude entityは、次のように変更できます.もちろん、すべてのentityをデータベースのテーブルにmapしたいわけではありません.Ignore()の方法を使用できます.
    public class SampleContext : DbContext
    {
        public DbSet Contacts { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Ignore();
            modelBuilder.Entity().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の方法:
    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; }
    }