Entity Framework7:生成无效的列名

Entity Framework 7 : Generating Invalid Column Name

使用 EF 7:1.0.0-rc1-final,

我在 EF 正确生成查询时遇到问题,使用数据库优先方法 - 使用 ef 脚手架生成 DbContext 中列出的一些模型属性 - 因为表包含大量列,我只需要几个为 webapi 工作,所以它们是列映射的

我有 3 个实体,品牌、活动和会议

品牌包含很多事件,事件包含很多会话

我的模特:

[Table("tblBranding")]
public class Brand 
{
    [Key]
    [Column("brandingId")]
    public int BrandId { get; set; }
    [Column("BrandingActive")]
    public bool Active { get; set; }
    [JsonIgnore]
    [Column("DeadBrand")]        
    public bool DeadBrand { get; set; }
    [Column("BrandingSiteTitle")]
    public string Name { get; set; }

    //navigation properties
    public virtual ICollection<Event> Events { get; set; }
}

[Table("tblEvents")]
public class Event
{           
    public int EventId { get; set; }
    [Column("eventActive")]
    public bool Active { get; set; }
    [Column("eventName")]
    public string Name { get; set; }        
    public DateTime EventCloseDate {get;set;}        
    public int PaxAllocationLimit { get; set; }

    //navigation properties             
    [JsonIgnore]        
    [Column("brandingId")]

    public virtual int BrandId { get; set; }
    [JsonIgnore]
    [ForeignKey("BrandId")]
    public virtual Brand Brand { get; set; }
    public virtual ICollection<Session> Sessions { get; set; }
}

[Table("tblEventsDates")]
public class Session
{       
    [Column("EventDateID")]
    public int SessionId { get; set; }
    [Column("EventDateName")]
    public string Name { get; set; }               
    [Column("EventDate")]
    public DateTime SessionDate { get; set; }        
    [Column("EventDateTime")]
    public DateTime SessionTime { get; set; }
    [Column("EventDateMinutes")]
    public decimal? SessionDurationInMinutes { get; set; }
    [Column("EventDateArrival")]
    public DateTime? ArrivalTime { get; set; }
    [Column("EventCapacity")]
    public int SessionCapacity { get; set; }

    //navigation properties        
    [JsonIgnore]        
    public virtual int EventId { get; set; }
    [JsonIgnore]        
    public virtual Event Event { get; set; }            
}

我的 DbContext

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{       
    modelBuilder.Entity<Event>()
        .HasOne(e => e.Brand)
        .WithMany(b => b.Events).HasForeignKey(e=>e.BrandId);

    modelBuilder.Entity<Event>()
        .HasMany(s => s.Sessions)
        .WithOne(e => e.Event).HasForeignKey(s => s.EventId);

    modelBuilder.Entity<Event>(entity=> {
        entity.Property(e => e.EventId).HasColumnName("EventID");
        entity.HasKey(e => new{ e.EventId, e.EventCloseDate});
        entity.HasIndex(e => e.EventId).HasName("For Full Text Indexing").IsUnique();
        entity.Property(e => e.Active).HasDefaultValue(false);
        entity.Property(e => e.EventCloseDate)
            .HasColumnType("datetime")
            .HasDefaultValueSql("'1/1/2038'");
        entity.Property(e => e.Name).HasMaxLength(1024);
        entity.Property(e => e.PaxAllocationLimit).HasDefaultValue(10000);
    });

    modelBuilder.Entity<Brand>(entity => {
        entity.HasKey(e => e.BrandId);                

        entity.Property(e => e.Active).HasDefaultValue(false);               

        entity.Property(e => e.Name)
            .IsRequired()
            .HasMaxLength(150)
            .HasColumnType("varchar");                
    });

    modelBuilder.Entity<Session>(entity => {
        entity.HasKey(e => e.SessionId);

        entity.Property(e=>e.Name)
          .HasMaxLength(250)
            .HasColumnType("varchar")
            .HasDefaultValue("");

        entity.Property(e => e.SessionDurationInMinutes)
            .HasColumnType("numeric")
            .HasDefaultValue(0m);                
        });
    }

    public virtual DbSet<Brand> Brands { get; set; }
    public virtual DbSet<Event> Events { get; set; }        
    public virtual DbSet<Session> Sessions { get; set; }
}

我将该项目用作 webapi,当我调用 Brands 时,它会生成以下内容 SQL:

SELECT [e].[brandingId], [e].[BrandingActive], [e].[DeadBrand], [e].[BrandingSiteTitle]
FROM [tblBranding] AS [e]
WHERE [e].[BrandingActive] = 1
ORDER BY [e].[BrandingSiteTitle], [e].[brandingId]
Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory: Information: Executed DbCommand (75ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [t].[EventId], [t].[EventCloseDate], [t].[eventActive], [t].[brandingId], [t].[EventId1], [t].[eventName], [t].[PaxAllocationLimit]
FROM [tblEvents] AS [t]
INNER JOIN (
    SELECT DISTINCT [e].[BrandingSiteTitle], [e].[brandingId]
    FROM [tblBranding] AS [e]
    WHERE [e].[BrandingActive] = 1
) AS [e] ON [t].[brandingId] = [e].[brandingId]
ORDER BY [e].[BrandingSiteTitle], [e].[brandingId]

其中 [t].[EventId1] 列名称无效 请注意,如果我在 DbContext 中注释掉以下代码 - 此错误消失并且正确生成查询:

modelBuilder.Entity<Event>()
            .HasMany(s => s.Sessions)
            .WithOne(e => e.Event).HasForeignKey(s => s.EventId);

我尝试添加 [ForeignKey]、[InverseProperty] 属性,而 'fiddling' - 它似乎没有什么不同

我也试过明确添加列名 here

我不确定还能尝试什么 - 只有当我在 FluidUI 中定义与会话的关系时它才会开始发生 - 建议?

回答我自己的问题 - 似乎它可能是 EF 7 1.0.0-RC1

中的错误

在 DbContext 中事件的实体属性中

 modelBuilder.Entity<Event>(entity=> {
    entity.Property(e => e.EventId).HasColumnName("EventID");
    entity.HasKey(e => new{ e.EventId, e.EventCloseDate});
    entity.HasIndex(e => e.EventId).HasName("For Full Text Indexing").IsUnique();
    entity.Property(e => e.Active).HasDefaultValue(false);
    entity.Property(e => e.EventCloseDate)
        .HasColumnType("datetime")
        .HasDefaultValueSql("'1/1/2038'");
    entity.Property(e => e.Name).HasMaxLength(1024);
    entity.Property(e => e.PaxAllocationLimit).HasDefaultValue(10000);
});

请注意,它有 2 个键 - 这是从脚手架生成的,table 有一个复合主键

然而,根据我对 API 的要求,我只需要一个主标识符 - 删除复合键修复了无效列生成错误

更新代码:

modelBuilder.Entity<Event>(entity=> {
    entity.Property(e => e.EventId).HasColumnName("EventID");
    entity.HasKey(e => e.EventId);
    entity.HasIndex(e => e.EventId).HasName("For Full Text Indexing").IsUnique();
    entity.Property(e => e.Active).HasDefaultValue(false);
    entity.Property(e => e.EventCloseDate)
        .HasColumnType("datetime")
        .HasDefaultValueSql("'1/1/2038'");
    entity.Property(e => e.Name).HasMaxLength(1024);
    entity.Property(e => e.PaxAllocationLimit).HasDefaultValue(10000);
});

纯属娱乐。尝试制作这个数据库。我删除了很多 "clutter" EF 就像 ASP.NET MVC 基于约定优于配置的概念。

您最多完成了 2 次配置(属性 and/or FluentApi),而实际上您可能已经完成了 0 次。

这里有一些关于约定的基本规则(约定对触发器不区分大小写)。

  • 要创建密钥(又名主键),您必须将其命名为 Id 或 [classname]Id
  • 要创建一个外键,您将其命名为 [foreighClassName(partA)]Id(如果您添加下一个则不需要此名称(而且您可以同时拥有两者,但名称必须相同(A 部分)
  • 要访问外来的"body"对象,只需要添加一个属性类型的public Brand Brand { get; set; }即可,如果只有一个[=48=,名称并不重要].
  • 在主要方面,您可能希望使用某种集合来包装所有子级,是的,public ICollection<Event> Events { get; set; } 是可行的方法。有人可能会问 IEnumerableIList 怎么样(可以这样想,IEnumerable 不能做 .Add() 所以它或多或少是只读的。 IList好吧,它做的更多,如果它不做一些无法翻译成 SQL 的事情,那将是一个很好的选择。所以在中间我们有 ICollection

什么时候使用virtual关键字?好吧,在 EF7 中,您不使用它,因为它用于启用延迟加载,而 EF7(目前)还没有,我们不知道他们是否要添加它。 Github EF7 feature request lacking lazyload

为什么我删除了 [JsonIgnore] 属性?切勿将实体模型发送给客户。创建适当的 DTO(在 ASP.NET MVC 中通常称为模型)

记得进行迁移,并且(为了好玩)首先尝试在 FluentAPI 中没有任何 "hardcoded" 要求,然后查看 mig 代码,您将看到 PK/FK 已完成、索引和其他几个 bob并为您添加了图钉。

public class Brand 
{
    public int Id { get; set; }
    public bool Active { get; set; }
    public bool DeadBrand { get; set; }
    public string Name { get; set; }

    //navigation properties
    public ICollection<Event> Events { get; set; }
}

public class Event
{           
    public int Id { get; set; }
    public bool Active { get; set; }
    public string Name { get; set; }        
    public DateTime EventCloseDate {get;set;}        
    public int PaxAllocationLimit { get; set; }

    //navigation properties             
    public Brand Brand { get; set; }
    public ICollection<Session> Sessions { get; set; }
}

public class Session
{       
    public int Id { get; set; }
    public string Name { get; set; }   
    //Datetime contains date and time        
    public DateTime Time { get; set; } 
    //TimeSpan is for duration, allowing access to seconds, minutes, hours etc.
    public TimeSpan Duration { get; set; } 
    public DateTime? ArrivalTime { get; set; }
    public int SessionCapacity { get; set; }

    //navigation properties        
    public Event Event { get; set; }            
}

上下文 class

class DbContex{

    public virtual DbSet<Brand> Brands { get; set; }
    public virtual DbSet<Event> Events { get; set; }        
    public virtual DbSet<Session> Sessions { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder) 
    {       
    //Personally I would not have many requirements in the database unless I              
    //was completely sure it had to be that way.
    //They will ALWAYS bite you in the ass in the long run.

        modelBuilder.Entity<Event>(entity=> {
            entity.Property(e => e.Active).HasDefaultValue(false);
            entity.Property(e => e.EventCloseDate)
                .HasColumnType("datetime")
                .HasDefaultValueSql("'1/1/2038'");
            entity.Property(e => e.Name).HasMaxLength(1024);
            entity.Property(e => e.PaxAllocationLimit).HasDefaultValue(10000);
        });

        modelBuilder.Entity<Brand>(entity => {
            entity.Property(e => e.Active).HasDefaultValue(false);    
            entity.Property(e => e.Name)
                .IsRequired()
                .HasMaxLength(150)
                .HasColumnType("varchar");                
        });

        modelBuilder.Entity<Session>(entity => {
            entity.Property(e=>e.Name)
                .HasMaxLength(250)
                .HasColumnType("varchar")
                .HasDefaultValue("");    
            entity.Property(e => e.SessionDurationInMinutes)
                .HasColumnType("numeric")
                .HasDefaultValue(0m);                
            });
        }
    }
}