te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>How to implement a database constraint within a C# Entity Framework (EF) class which applies the Table Per Concrete Type (TPC) s
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to implement a database constraint within a C# Entity Framework (EF) class which applies the Table Per Concrete Type (TPC) s

programmeradmin3浏览0评论

I have an ASP.NET Core web application written in C# that utilizes the Table-Per-Concrete-Type (TPC) strategy in its database structure.

For simplicity, let's say that my application solely has 3 tables: A, B, and C.

Regarding Table A, it has two columns (C1 and C2), plus a unique identifier column (Id). C1 is a nullable DateOnly column; C2 is a non-nullable DateOnly column.

Tables B and C have columns not relevant to this question; thus are omitted from the discussion.

It is worth mentioning that table A is mapped to a base reference type named "Class_A". Tables B and C are mapped to classes "Class_B" and "Class_C", respectively. Finally, Class_B and Class_C are direct descendants of Class_A.

Constraint

One of my business cases implies a database constraint over Table A. Specifically, column C1 must be greater than C2 when C1 is not null.

Question

Given the above context, how could one implement a constraint over Table_A using fluent API during modelBuilding? This constraint should be directly applicable and inheritable by its descendant tables.

It is worth mentioning that implementations like the example below cause database errors due to constraint duplicity (as already shown elsewhere). Meanwhile, Microsoft Documentation does not cite any potential constraint conflict (see here) due to TPC strategy (see here, here, and here).

Constraint Implementation (code example)

namespace MyApplication.DataPersistance

public class Class_A
{
    public DateOnly? C1 { get; set; }
    public DateOnly  C2 { get; set; }
}

public class Class_B : Class_A
{
    // ... other properties
}

public class Class_C : Class_A
{
    // ... other properties
}

public class ApplicationDbContext : IdentityDbContext
{
    public DbSet<Class_A> A { get; set; }
    public DbSet<Class_B> B { get; set; }
    public DbSet<Class_C> C { get; set; }

    // ... other DbSets

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Class_A>(entity => {
             entity.ToTable(b => b.HasCheckConstraint("CK_Class_A_C1_gt_C2", "[C1] > [C2]"));         
             entity.UseTpcMappingStrategy();
        });

        modelBuilder.Entity<Class_B>(entity => { });

        modelBuilder.Entity<Class_C>(entity => { });

        // ... configuration of other tables ...
    }
}

Conclusion

So far, the TPC strategy seems to have some limitations regarding database constraint implementations. Nevertheless, I am eager to know Stackoverflow's community opinion and experience in the subject.

Sincerely,

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论