带有可选导航的 EF Core 中的自有类型
Owned Type in EF Core with optional navigation
下面是我的class
class Company
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public int? CityId { get; set; }
public City City { get; set; }
public int CountryId { get; set; }
public Country Country { get; set; }
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
}
我的问题是:
我可以将地址 class 设为 OwnedType 因为城市 ID 是可选的
如果我将地址设为自有类型,那么我的城市 class 也需要是自有类型吗?
提前致谢了。使用 ef 核心 3.1
您可以像这样将 Address
class 设置为 OwnedType。
地址class:
public class Address
{
public string Street { get; set; }
public int? CityId { get; set; }
public City City { get; set; }
public Company Company { get; set; }
}
公司Class:
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
流利Api:
public class YourDbContext : DbContext
{
public YourDbContext (DbContextOptions<YourDbContext> options)
: base(options)
{
}
//Note:no need add Address DbSet
public DbSet<City> City { get; set; }
public DbSet<Company> Company { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>().OwnsOne(c => c.Address);
}
}
if i make Address as owned type then my City class also need to be owned type?
您的城市class不需要拥有类型。
参考:
https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities#explicit-configuration
结果:
当您配置拥有的类型时,这会使 company
和 address
生成一个 table 并且可以设置城市 ID 或者 not.And 如果您想设置城市id,你需要在你的city
table.
中插入一条新记录
下面是我的class
class Company
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public int? CityId { get; set; }
public City City { get; set; }
public int CountryId { get; set; }
public Country Country { get; set; }
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
}
我的问题是:
我可以将地址 class 设为 OwnedType 因为城市 ID 是可选的
如果我将地址设为自有类型,那么我的城市 class 也需要是自有类型吗?
提前致谢了。使用 ef 核心 3.1
您可以像这样将 Address
class 设置为 OwnedType。
地址class:
public class Address
{
public string Street { get; set; }
public int? CityId { get; set; }
public City City { get; set; }
public Company Company { get; set; }
}
公司Class:
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
流利Api:
public class YourDbContext : DbContext
{
public YourDbContext (DbContextOptions<YourDbContext> options)
: base(options)
{
}
//Note:no need add Address DbSet
public DbSet<City> City { get; set; }
public DbSet<Company> Company { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>().OwnsOne(c => c.Address);
}
}
if i make Address as owned type then my City class also need to be owned type?
您的城市class不需要拥有类型。
参考:
https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities#explicit-configuration
结果:
当您配置拥有的类型时,这会使 company
和 address
生成一个 table 并且可以设置城市 ID 或者 not.And 如果您想设置城市id,你需要在你的city
table.