簡體   English   中英

EF Core 3.1 cosmosdb 擁有的實體

[英]EF Core 3.1 cosmosdb Owned Entities

我有以下域 model,我試圖確保它是使用 EF Core 3.1 的 COSMOSDB 提供程序創建的,但是我一直收到錯誤消息。

Model 類:

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:無法將類型“地址”標記為擁有,因為已存在同名的非擁有實體類型。

我如何 map 這種關系,所以我可以得到一個 Json 文件,如:

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

因此,在efcore github上發布后,結果是調用:

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

將地址類型配置為標准實體而不是擁有實體。 我有效地嘗試在一個 go 中配置地址類型的所有用途。 這目前是不可能的,需要在每次使用該類型時完成,例如:

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 中預期能夠進行批量配置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM