EF Core 3.1 cosmosdb拥有的实体

时间:2020-07-22 22:26:33

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

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

模型类:

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();
}

DbContext:

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 }
}

1 个答案:

答案 0 :(得分:1)

因此,在efcore github上发布后,事实证明该调用是

select count(*) no_mismatches
from (select state from mytable group by state having min(state) <> max(state)) t

将“地址类型”配置为标准实体,而不是“拥有”实体。我实际上是一次尝试配置地址类型的所有用途。目前这是不可能的,需要对每种类型的用法进行操作,例如:

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

在EFCore 6.0中有望进行批量配置