【问题标题】:EF Core 3.1 cosmosdb Owned EntitiesEF Core 3.1 cosmosdb 拥有的实体
【发布时间】:2020-07-22 22:26:33
【问题描述】:

我尝试确保使用 COSMOSDB 提供程序为 EF Core 3.1 创建以下域模型,但是我不断收到错误消息。

模型类:

public class Application
{
    public Site Site { get; set; } = new Site();
    public Applicant Applicant { get; set; } = new Applicant();
    public Agent Agent { get; set; } = new Agent();
}

public class Site
{
    public Address Address { get; set; } = new Address();
}

public class Applicant
{
    public Address Address { get; set; } = new Address();
}

public class Agent
{
    public Address Address { get; set; } = new Address();
}

public class Address
{
    public MapCoordinate MapCoordinate { get; set; } = new MapCoordinate();

    public GeoLocation GeoLocation { get; set; } = new GeoLocation();
}

数据库上下文:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{   
    modelBuilder.HasDefaultContainer("Applications");

    modelBuilder.Entity<Address>(x => x.Property(y => y.GeoLocation).HasJsonConversion());
            
    modelBuilder.Entity<Application>(x =>
            {
                x.HasKey(x => x.ApplicationId);
                x.HasPartitionKey(x => x.ApplicationId);
                
                x.OwnsOne(x => x.Site).OwnsOne(x => x.Address);
                x.OwnsOne(x => x.Applicant).OwnsOne(x => x.Address);
                x.OwnsOne(x => x.Agent).OwnsOne(x => x.Address);
            });
}

错误:在线

x.OwnsOne(x => x.Site).OwnsOne(x => x.Address)

我明白了:

System.InvalidOperationException:无法将类型“地址”标记为拥有,因为已存在同名的非拥有实体类型。

我如何映射这种关系,以便我可以得到一个 Json 文档,例如:

{ 
    site : { address: { MapCoordinate : blah, GeoLocation: blah },
    applicant : { address: { MapCoordinate : blah, GeoLocation: blah },
    agent : { address: { MapCoordinate : blah, GeoLocation: blah }
}

【问题讨论】:

    标签: c# azure-cosmosdb ef-core-3.1


    【解决方案1】:

    所以在efcore github 发帖后,原来是调用:

    modelBuilder.Entity<Address>(x => x.Property(y => y.GeoLocation).HasJsonConversion());
    

    将地址类型配置为标准实体而不是拥有实体。我有效地尝试一次性配置地址类型的所有用途。这目前是不可能的,需要在每次使用该类型时完成,例如:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.HasDefaultContainer("Applications");
    
                modelBuilder.Entity<Application>(x =>
                {
                    x.HasKey(x => x.ApplicationId);
                    x.HasPartitionKey(x => x.ApplicationId);
                    
                    x.OwnsOne(x => x.Site).OwnsOne(x => x.Address, y =>
                        {
                            y.Property(p => p.GeoLocation).HasJsonConversion();
                            y.OwnsOne(p => p.MapCoordinate);
                        });
                    x.OwnsOne(x => x.Applicant).OwnsOne(x => x.Address, y =>
                    {
                        y.Property(p => p.GeoLocation).HasJsonConversion();
                        y.OwnsOne(p => p.MapCoordinate);
                    });
                    x.OwnsOne(x => x.Agent).OwnsOne(x => x.Address, y =>
                    {
                        y.Property(p => p.GeoLocation).HasJsonConversion();
                        y.OwnsOne(p => p.MapCoordinate);
                    });
                });
            }
    

    EFCore 6.0 有望支持批量配置

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多