Entity Framework ModelBuilder 在两个不同的文件中?
Entity Framework ModelBuilder in two different files?
是否可以将实体的模型构建器放在不同的文件中?我们正在尝试将自动脚手架与数据库分开,同时也有一定的手动定制。
这就是我正在尝试的,收到以下错误,
文件 1:
modelBuilder.Entity<PropertyMailingAddress>(entity =>
{
entity.HasOne(d => d.LkAddressType)
.WithMany(p => p.PropertyMailingAddress)
.HasForeignKey(d => d.LkAddressTypeId)
.HasConstraintName("FK_PropertyMailingAddress_LK_AddressTypeId");
文件 2:
modelBuilder.Entity<PropertyMailingAddress>(entity =>
{
entity.HasOne(d => d.LkSourceOfAddress)
.WithMany(p => p.PropertyMailingAddress)
.HasForeignKey(d => d.LkSourceOfAddressId)
.HasConstraintName("FK_PropertyMailingAddress_LK_SourceOfAddressId");
Error CS1501 No overload for method 'Entity' takes 2 arguments
是否可以为此提供部分 class 方法?
我真的不知道你为什么要为单个实体创建单独的配置文件。但是,根据您提供的内容,您可以这样做:
public partial static class MyExtensions
{
public static void ConfigureMyEntity(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>()
.HasOne<Other>()
.WithMany()
....
}
}
如果您想为单独的实体定义配置,可以使用更简洁的方法。
如果您想为每个实体创建一个文件,那么不要使用模型构建器,而是使用 IEntityTypeConfiguration
。为每个实体创建一个 class 实现 IEntityTypeConfiguration<T>
。例如:
public class PropertyMailingAddressConfig : IEntityTypeConfiguration<PropertyMailingAddress>
{
public void Configure(EntityTypeBuilder<PropertyMailingAddress> builder)
{
builder
.HasOne(d => d.LkAddressType);
.WithMany(p => p.PropertyMailingAddress);
.HasForeignKey(d => d.LkAddressTypeId)
.HasConstraintName("FK_PropertyMailingAddress_LK_AddressTypeId");
}
}
默认约定以相同的方式应用,因此 属性 调用的 Id 将自动映射为主键等。
然后您需要告诉 Entity Framework 从给定 Assembly
:
中找到的文件加载实体配置
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
您可以通过将 dbcontext class 设置为 partial
,在两个文件中创建部分 class,然后在其中一个文件中定义一个方法来传递ModelBuilder
个参数。
详情请参考下文
文件 1:
public partial class MyDbContext: DbContext
{
public DbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
public DbSet<PropertyMailingAddress> PropertyMailingAddress{ get; set; }
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
OnModelCreatingPartial(modelBuilder);
modelBuilder.Entity<PropertyMailingAddress>(entity =>
{
entity.HasOne(d => d.LkAddressType)
.WithMany(p => p.PropertyMailingAddress)
.HasForeignKey(d => d.LkAddressTypeId)
.HasConstraintName("FK_PropertyMailingAddress_LK_AddressTypeId");
});
}
}
文件 2:
public partial class MyDbContext : DbContext
{
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PropertyMailingAddress>(entity =>
{
entity.HasOne(d => d.LkSourceOfAddress)
.WithMany(p => p.PropertyMailingAddress)
.HasForeignKey(d => d.LkSourceOfAddressId)
.HasConstraintName("FK_PropertyMailingAddress_LK_SourceOfAddressId");
});
}
}
可以参考.
是否可以将实体的模型构建器放在不同的文件中?我们正在尝试将自动脚手架与数据库分开,同时也有一定的手动定制。
这就是我正在尝试的,收到以下错误,
文件 1:
modelBuilder.Entity<PropertyMailingAddress>(entity =>
{
entity.HasOne(d => d.LkAddressType)
.WithMany(p => p.PropertyMailingAddress)
.HasForeignKey(d => d.LkAddressTypeId)
.HasConstraintName("FK_PropertyMailingAddress_LK_AddressTypeId");
文件 2:
modelBuilder.Entity<PropertyMailingAddress>(entity =>
{
entity.HasOne(d => d.LkSourceOfAddress)
.WithMany(p => p.PropertyMailingAddress)
.HasForeignKey(d => d.LkSourceOfAddressId)
.HasConstraintName("FK_PropertyMailingAddress_LK_SourceOfAddressId");
Error CS1501 No overload for method 'Entity' takes 2 arguments
是否可以为此提供部分 class 方法?
我真的不知道你为什么要为单个实体创建单独的配置文件。但是,根据您提供的内容,您可以这样做:
public partial static class MyExtensions
{
public static void ConfigureMyEntity(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>()
.HasOne<Other>()
.WithMany()
....
}
}
如果您想为单独的实体定义配置,可以使用更简洁的方法。
如果您想为每个实体创建一个文件,那么不要使用模型构建器,而是使用 IEntityTypeConfiguration
。为每个实体创建一个 class 实现 IEntityTypeConfiguration<T>
。例如:
public class PropertyMailingAddressConfig : IEntityTypeConfiguration<PropertyMailingAddress>
{
public void Configure(EntityTypeBuilder<PropertyMailingAddress> builder)
{
builder
.HasOne(d => d.LkAddressType);
.WithMany(p => p.PropertyMailingAddress);
.HasForeignKey(d => d.LkAddressTypeId)
.HasConstraintName("FK_PropertyMailingAddress_LK_AddressTypeId");
}
}
默认约定以相同的方式应用,因此 属性 调用的 Id 将自动映射为主键等。
然后您需要告诉 Entity Framework 从给定 Assembly
:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
您可以通过将 dbcontext class 设置为 partial
,在两个文件中创建部分 class,然后在其中一个文件中定义一个方法来传递ModelBuilder
个参数。
详情请参考下文
文件 1:
public partial class MyDbContext: DbContext
{
public DbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
public DbSet<PropertyMailingAddress> PropertyMailingAddress{ get; set; }
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
OnModelCreatingPartial(modelBuilder);
modelBuilder.Entity<PropertyMailingAddress>(entity =>
{
entity.HasOne(d => d.LkAddressType)
.WithMany(p => p.PropertyMailingAddress)
.HasForeignKey(d => d.LkAddressTypeId)
.HasConstraintName("FK_PropertyMailingAddress_LK_AddressTypeId");
});
}
}
文件 2:
public partial class MyDbContext : DbContext
{
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PropertyMailingAddress>(entity =>
{
entity.HasOne(d => d.LkSourceOfAddress)
.WithMany(p => p.PropertyMailingAddress)
.HasForeignKey(d => d.LkSourceOfAddressId)
.HasConstraintName("FK_PropertyMailingAddress_LK_SourceOfAddressId");
});
}
}
可以参考