EF Core 3.1 cosmosdb 拥有的实体
EF Core 3.1 cosmosdb Owned Entities
我试图确保使用 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();
}
数据库上下文:
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: The type 'Address' cannot be marked as owned because a non-owned entity type with the same name already exists.
如何映射此关系,以便获得 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());
将地址类型配置为标准实体而不是拥有实体。我实际上是在尝试一次性配置地址类型的所有用途。这目前是不可能的,需要在每次使用类型时完成,例如:
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 将具备批量配置功能
我试图确保使用 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();
}
数据库上下文:
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: The type 'Address' cannot be marked as owned because a non-owned entity type with the same name already exists.
如何映射此关系,以便获得 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());
将地址类型配置为标准实体而不是拥有实体。我实际上是在尝试一次性配置地址类型的所有用途。这目前是不可能的,需要在每次使用类型时完成,例如:
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 将具备批量配置功能