EFCore Map 2 entities to same table

前端 未结 3 449
渐次进展
渐次进展 2020-12-11 02:27

I\'m trying to use DDD with EFCore and I am struggling to find a way to map 2 POCOs from different context that represent the same entity to the same table.

I have a

3条回答
  •  感动是毒
    2020-12-11 03:02

    I am looking at this problem myself. I noticed, that if you specify schema name for one of the tables then EF will not complain. For example in your case:

    modelBuilder.Entity(u =>
        {
            u.ToTable("User", "dbo").HasKey(e => e.Id);
            u.OwnsOne(e => e.Name);
            u.OwnsOne(b => b.HomeAddress);
        });
    
        modelBuilder.Entity(u =>
        {
            u.ToTable("User").HasKey(e => e.Id);
        });
    

    Of course this is not a full solution and even not a workaround, since you can not have more than 2 mentions of "User" table (that is, in more than 2 contexts).

    Also i found https://data.uservoice.com/forums/72025-entity-framework-core-feature-suggestions/suggestions/1872001-map-multiple-entities-to-same-table which makes me think that this in general is not possible.

    Regarding DDD in general

    Most sources say that your bounded contexts should be isolated not only by code, but also by data. This in theory means, that your User table should be duplicated in each bounded context. This is ideal way, but is unnecessarily complex (imho) for more simple scenarios, since it involves data synchronization across all duplicated tables.

提交回复
热议问题