【发布时间】: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