Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于Entity Framework Core加载聚合根问题 #8

Open
huangchangmingxinghui opened this issue Jun 16, 2020 · 0 comments
Open

关于Entity Framework Core加载聚合根问题 #8

huangchangmingxinghui opened this issue Jun 16, 2020 · 0 comments

Comments

@huangchangmingxinghui
Copy link

huangchangmingxinghui commented Jun 16, 2020

肖老师:您好!
最近做项目时,在使用Entity Framework Core配置好聚合根映射关系后,发现在生成表及加载数据时有很多问题,希望能听听您的意见。
场景:因为聚合根拥有一些从属值对象,并且值对象内还会有嵌套,根据官方文档,这类映射配置是没有问题的,并且从属的值对象都和聚合根对象保存到一张表里(一对一关系),映射配置如以下:
buillder.ToTable("table_name");
builder.HasKey(p => p.Id);
builder.Property(p => p.Name).IsRequired().HasMaxLength(20).HasColumnName("name");
builder.OwnsOne(p => p.XXX, x => {
x.Porperty(p => p.aa).IsRequired().HasMaxLength(5).HasColumnName("aa");
x.OwnsOne(p => p.YYY, y => {
y.Property(p => p.bb).IsRequired().HasMaxLength(20).HasColumnName("bb");
y.OwsOne(p => p.ZZZ, z => {
z.Property(p => p.cc).IsRequired().HasMaxLength(50).HasColumnName("cc");
});
});
});

现在的问题是:

  1. 从属值对象类型中配置的 IsRequired()无法生效,生成的表字段仍然可为Null,但在查询时,框架生成的SQL语句包含WHERE Filed NOT NULL的条件方式查询。IsRequired()只对聚合根上基本类型才会生效,即上面的 Name 属性。怎么会这样?
  2. 当加载聚合根对象时,生成的SQL语句对每个从属值对象类型都会在内部生成一个子表的方式,然后再和主表通过 LEFT JOIN方式连接,因为本身值对象中又嵌套值对象,并且可能很多(毕竟在设计聚合根时,都会把不变条件中涉及的内容封装成对应的值对象,这本来也是领域驱动设计的建模原则。但带来的问题是,Entity Framework Core加载这种聚合根时,大量嵌套的Left Join方式,这对性能会产生很大的影响,这种情况应该如何处理?是实体框架本身的问题还是设计上有什么问题?

以上问题在网上也没查到对应的问题说明,更别说解决方案了,希望老师能予以解答说明,谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant