Entity Framework:如何对具有大量属性的类进行分组

huangapple go评论71阅读模式
英文:

Entity Framework: How to group class with lots of properties

问题

我继承了一个相当大的EF Core应用程序,其中一些表格有近90行。这些行大多数是相关的,例如,仅针对配置就有20行。有没有一种优雅的方法来将其组织成类?

例如,当前的伪代码:

public class Annoying
{
   public string A { get; set; }
   ...
   public string Z { get; set; }

   public string ConfigA { get; set; }
   ...
   public string ConfigZ { get; set; }
}

我想将其转换为如下所示:

public class Annoying
{
   public string A { get; set; }
   ...
   public string Z { get; set; }

   public Configuration Config { get; set; } // 包含所有配置属性
}

在.NET 7+ EF Core Code First上有没有一个好的方法来做到这一点?

英文:

I inherited a largish EF Core application, where some Table have nearly 90 rows. Most of these rows are related, e.g. there 20 rows for Configuration alone. Is there an elegant way to organize this into classes?

e.g. current Pseudo code:

public class Annoying
{
   public string A {get; set;}
   ...
   public string Z {get; set;}

   public string ConfigA {get; set;}
   ...
   public string ConfigZ {get; set;}
}

I would like it to transform it like so:

public class Annoying
{
   public string A {get; set;}
   ...
   public string Z {get; set;}

   public Configuration Config {get; set;} // Contains all Config Properties

}

Is there a good way to do this on .NET 7 + EF Core Code first?

答案1

得分: 4

你可以考虑使用Owned Entity Types。类似于:

public class Annoying
{
   public string A {get; set;}
   ...
   public string Z {get; set;}

   public Configuration Config {get; set;} // 包含所有配置属性
}

[Owned]
public class Configuration
{
    public string A { get; set; }
    public string Z { get; set; }
}

请注意,默认情况下,它将导致列包含前缀和属性名称,由下划线分隔,即像 Config_A,您可以使用自定义列名称映射来“移除”前缀和/或下划线:

modelBuilder.Entity<Annoying>()
	.OwnsOne(p => p.Config, 
		owned =>
		{
            // 可能应该自动化:
			owned.Property(cfg => cfg.A).HasColumnName("ConfigA");
            owned.Property(cfg => cfg.B).HasColumnName("ConfigB");
            // ...
		});
英文:

You can look into using Owned Entity Types. Something like:

public class Annoying
{
   public string A {get; set;}
   ...
   public string Z {get; set;}

   public Configuration Config {get; set;} // Contains all Config Properties
}

[Owned]
public class Configuration
{
    public string A { get; set; }
    public string Z { get; set; }
}

Note that by default it will lead to columns with consisting of prefix and property name split by underscore i.e. like Config_A, you can "remove" the prefix and/or underscore with custom column name mapping:

modelBuilder.Entity<Annoying>()
	.OwnsOne(p => p.Config, 
		owned =>
		{
            // possibly should be automated:
			owned.Property(cfg => cfg.A).HasColumnName("ConfigA");
            owned.Property(cfg => cfg.B).HasColumnName("ConfigB");
            // ...
		});

答案2

得分: 0

你是否在寻找一个包装类?类似这样的东西

var AnnoyingGrouped = context.Annoying.GroupBy((x, y) =>
             x.ConfigA == y.ConfigA && x.ConfigB == y.ConfigB);

现在你已经按配置(键)分组了。你如何使用它取决于你。

英文:

Are you looking for a wrapper class? something like this

var AnnoyingGrouped = context.Annoying.GroupBy((x,y) => 
         x.ConfigA == y.ConfigA && x.ConfigB == y.ConfigB);

Now you have grouped by Configuration (the Key). How you use it is up to you.

huangapple
  • 本文由 发表于 2023年7月11日 13:47:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定