gpt4 book ai didi

c# - EF Core 映射 EntityTypeConfiguration

转载 作者:IT王子 更新时间:2023-10-29 03:35:25 26 4
gpt4 key购买 nike

在EF6中我们通常可以使用这种方式来配置Entity。

public class AccountMap : EntityTypeConfiguration<Account>
{
public AccountMap()
{
ToTable("Account");
HasKey(a => a.Id);

Property(a => a.Username).HasMaxLength(50);
Property(a => a.Email).HasMaxLength(255);
Property(a => a.Name).HasMaxLength(255);
}
}

我们如何在 EF Core 中做,因为当我继承 EntityTypeConfiguration 类时无法找到该类。

我从 GitHub 下载 EF Core 原始源代码,但找不到。有人可以帮忙吗?

最佳答案

自 EF Core 2.0 以来有 IEntityTypeConfiguration<TEntity> .你可以像这样使用它:

class CustomerConfiguration : IEntityTypeConfiguration<Customer>
{
public void Configure(EntityTypeBuilder<Customer> builder)
{
builder.HasKey(c => c.AlternateKey);
builder.Property(c => c.Name).HasMaxLength(200);
}
}

...
// OnModelCreating
builder.ApplyConfiguration(new CustomerConfiguration());

有关此功能和 2.0 中引入的其他新功能的更多信息,请参见 here .

关于c# - EF Core 映射 EntityTypeConfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26957519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com