Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将modelBuilder配置为一次组合多个属性配置_C#_Entity Framework - Fatal编程技术网

C# 将modelBuilder配置为一次组合多个属性配置

C# 将modelBuilder配置为一次组合多个属性配置,c#,entity-framework,C#,Entity Framework,是否有一种方法可以为Required、MaxLength和HasColumn组合多个属性,或者需要为每个属性创建一个属性 我希望能够包括多个需要的字段,如果它们是相同的MaxLength,则将其分配给它们,而不是像我现在所做的那样为实体中的每个字段创建一个新字段 public class DataEntryContext : DbContext { public DataEntryContext(DbContextOptions<DataEntryContext> optio

是否有一种方法可以为Required、MaxLength和HasColumn组合多个属性,或者需要为每个属性创建一个属性

我希望能够包括多个需要的字段,如果它们是相同的MaxLength,则将其分配给它们,而不是像我现在所做的那样为实体中的每个字段创建一个新字段

public class DataEntryContext : DbContext
{
    public DataEntryContext(DbContextOptions<DataEntryContext> options)
        :base (options)
    { }

    public DbSet<Employee> Employees { get; set; }
    public DbSet<Department> Departments { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Employee>()
            .HasKey(e => e.EmpId);

        modelBuilder.Entity<Employee>()
            .Property(e => e.EmpFirstName)
            .HasColumnType("varchar(50)")
            .HasMaxLength(50)
            .IsRequired();

        modelBuilder.Entity<Employee>()
            .Property(e => e.EmpLastName)
            .HasColumnType("varchar(50)")
            .HasMaxLength(50)
            .IsRequired();

        modelBuilder.Entity<Employee>()
            .Property(e => e.EmpPhoneNumber)
            .HasColumnType("varchar(10)")
            .HasMaxLength(10)
            .IsRequired();

        modelBuilder.Entity<Employee>()
            .Property(e => e.EmpStartDate)
            .HasColumnType("datetime")
            .IsRequired();


        modelBuilder.Entity<Department>()
            .HasKey(d => d.DeptId);

        modelBuilder.Entity<Department>()
            .Property(d => d.DeptName)
            .HasColumnType("varchar(50)")
            .HasMaxLength(50)
            .IsRequired();

    }
}
公共类DataEntryContext:DbContext { 公共DataEntryContext(DbContextOptions) :基本(选项) { } 公共数据库集雇员{get;set;} 公共数据库集部门{get;set;} 模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder) { modelBuilder.Entity() .HasKey(e=>e.EmpId); modelBuilder.Entity() .Property(e=>e.EmpFirstName) .HasColumnType(“varchar(50)”) .HasMaxLength(50) .IsRequired(); modelBuilder.Entity() .Property(e=>e.EmpLastName) .HasColumnType(“varchar(50)”) .HasMaxLength(50) .IsRequired(); modelBuilder.Entity() .Property(e=>e.EmpPhoneNumber) .HasColumnType(“varchar(10)”) .HasMaxLength(10) .IsRequired(); modelBuilder.Entity() .Property(e=>e.EmpStartDate) .HasColumnType(“日期时间”) .IsRequired(); modelBuilder.Entity() .HasKey(d=>d.DeptId); modelBuilder.Entity() .Property(d=>d.DeptName) .HasColumnType(“varchar(50)”) .HasMaxLength(50) .IsRequired(); } }
不,目前还不可能。你必须为每一处房产写上这句话。您最多可以强制Entity Framework将.Net数据类型映射到特定的MS SQL数据类型,如图所示。

当然可以,您只需自己编写代码即可。例如,我编写了一个
EntityTypeConfigurationExtensions
,它允许您在一次调用中使用多个属性配置实体,而不是多次调用。我不明白为什么您不能修改我的代码以使用
params
,然后您可以传递多个属性:

(必须先进行propertyConfiguration,然后再进行propertyExpression)


使用字符串varchar50=“varchar(50)”;并使用modelBuilder.Entity().Property(d=>d.DeptName).HasColumnType(varchar50).HasMaxLength(50).IsRequired();这样的尝试往往会降低代码的灵活性。有一个不一致,一个最大长度为10(而不是50)的必填字段。您将如何处理?感谢您的回复,但我想知道,如果可能的话,如何在一个实体中只调用一次就包含多个字段。也许我解释得不对。请看我想知道的是可能还是不可能<代码>modelBuilder.Entity().Property(e=>e.EmpFirstName),(e=>e.EmpLastName).HasColumnType(“varchar(50)”).IsRequired()
public static class EntityTypeConfigurationExtensions
{
    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, byte[]>> propertyExpression,
        Func<BinaryPropertyConfiguration, BinaryPropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, Guid>> propertyExpression,
        Func<PrimitivePropertyConfiguration, PrimitivePropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, Guid?>> propertyExpression,
        Func<PrimitivePropertyConfiguration, PrimitivePropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, TimeSpan?>> propertyExpression,
        Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, TimeSpan>> propertyExpression,
        Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, DateTimeOffset?>> propertyExpression,
        Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, DateTimeOffset>> propertyExpression,
        Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, DateTime?>> propertyExpression,
        Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, DateTime>> propertyExpression,
        Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, decimal?>> propertyExpression,
        Func<DecimalPropertyConfiguration, DecimalPropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, decimal>> propertyExpression,
        Func<DecimalPropertyConfiguration, DecimalPropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, string>> propertyExpression,
        Func<StringPropertyConfiguration, StringPropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        propertyConfiguration(instance.Property(propertyExpression));

        return instance;
    }
}
modelBuilder.Entity<Employee>()
  .HasKey(e => e.EmpId);

modelBuilder.Entity<Employee>()
  .Property(e => e.EmpFirstName)
  .HasColumnType("varchar(50)")
  .HasMaxLength(50)
  .IsRequired();

modelBuilder.Entity<Employee>()
  .Property(e => e.EmpLastName)
  .HasColumnType("varchar(50)")
  .HasMaxLength(50)
  .IsRequired();

modelBuilder.Entity<Employee>()
  .Property(e => e.EmpStartDate)
  .HasColumnType("datetime")
  .IsRequired();
modelBuilder.Entity<Employee>()
  .HasKey(e => e.EmpId)
  .Property(e => e.EmpFirstName,
    p => p.HasColumnType("varchar(50)")
     .HasMaxLength(50)
     .IsRequired())
  .Property(e => e.EmpLastName,
    p => p.HasColumnType("varchar(50)")
      .HasMaxLength(50)
      .IsRequired())
  .Property(e => e.EmpStartDate,
    p => p.HasColumnType("datetime")
      .IsRequired());
modelBuilder.Entity<Employee>()
  .HasKey(e => e.EmpId)
  .Property(p => p.HasColumnType("varchar(50)")
    .HasMaxLength(50)
    .IsRequired(),
    e => e.EmpFirstName,
    e => e.EmpLastName);
  .Property(p => p.HasColumnType("datetime")
    .IsRequired(),
    e => e.EmpStartDate,);