首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实体框架7:生成无效的列名

实体框架7:生成无效的列名
EN

Stack Overflow用户
提问于 2016-04-13 08:35:03
回答 2查看 18.3K关注 0票数 10

使用EF 7: 1.0.0-rc1-final,

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

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

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

我的模型:

代码语言:javascript
复制
[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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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中注释掉了以下代码,这个错误就会消失,查询就会正确生成:

代码语言:javascript
复制
modelBuilder.Entity<Event>()
            .HasMany(s => s.Sessions)
            .WithOne(e => e.Event).HasForeignKey(s => s.EventId);

我试着在‘摆弄’的时候添加ForeignKey、InverseProperty属性--似乎没有什么不同

我还尝试像前面提到的here那样显式地添加列名

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-13 19:09:07

只是为了好玩。试着创建这个数据库。我去掉了很多“杂物”EF类似于基于约定重于配置的概念的ASP.NET MVC。

你已经做了2次配置(属性和/或FluentApi),而实际上你可以做0次。

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

  • 要创建一个键(又称主键),您必须将其命名为Id或classnameId
  • 以创建一个外键,您可以将其命名为foreighClassName(partA)Id (如果您添加下一个键,则不需要此Id(并且ofc您可以同时拥有这两个键,但之后名称必须是相同的(PartA)
  • 才能访问外部"body“对象,您只需添加一个类似以下public Brand Brand { get; set; }类型的属性,如果只有一个“链接”,那么名称并不重要。
  • 在第一端,你可能想要使用某种集合来包装所有的子元素,是的,public ICollection<Event> Events { get; set; }是一种方法。有人可能会问IEnumerableIList是怎么回事(这么想吧,IEnumerable不能做.Add(),所以它或多或少是只读的。它可以做更多的事情,如果它不是在做一些无法翻译到IList的事情,那将是一个很好的选择。所以在中间我们有ICollection

当你使用虚拟关键字的时候?在EF7中你不会使用它,因为它是用来实现延迟加载的,而EF7 (目前)还没有这个功能,我们不知道他们是否会添加它。Github EF7 feature request lacking lazyload

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

记得做迁移,(为了好玩)先试一试,在FluentAPI中没有任何“硬编码”的要求,看看mig代码,你会看到PK/FK已经完成,索引和其他几个bob和pin都为你添加了。

代码语言:javascript
复制
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; }            
}

上下文类

代码语言:javascript
复制
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);                
            });
        }
    }
}
票数 4
EN

Stack Overflow用户

发布于 2016-04-13 10:07:59

回答我自己的问题-它似乎是EF 7 1.0.0-RC1中的一个错误

在DbContext中事件的实体属性中

代码语言:javascript
复制
 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);
});

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

但是,对于API的要求,我只需要一个主标识符-删除组合键修复了无效列生成错误

更新代码:

代码语言:javascript
复制
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);
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36586487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档