sử dụng EF 7: 1.0.0-rc1 kết,Entity Framework 7: Tạo không hợp lệ Cột Tên
Tôi đang gặp rắc rối với EF tạo ra các truy vấn đúng cách, sử dụng cơ sở dữ liệu tiếp cận đầu tiên - sử dụng giàn giáo ef để tạo một số tính chất mô hình được liệt kê trong DbContext - như các bảng có chứa một số lượng lớn các cột tôi chỉ cần một vài làm việc cho WebAPI để họ được cột ánh xạ
tôi có 3 đối tượng, thương hiệu, sự kiện và phiên
Thương hiệu chứa nhiều Sự kiện và Sự kiện chứa nhiều Phiên
mô hình của tôi:
[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; }
}
My 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; }
}
Tôi đang sử dụng các dự án như một WebAPI, khi tôi gọi lên Brands, nó tạo ra các SQL sau:
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]
trong đó [t]. [EventId1] tên cột không hợp lệ lưu ý rằng nếu tôi nhận xét mã sau trong DbContext - lỗi này biến mất và truy vấn được tạo chính xác :
modelBuilder.Entity<Event>()
.HasMany(s => s.Sessions)
.WithOne(e => e.Event).HasForeignKey(s => s.EventId);
Tôi đã thử thêm [ForeignKey], [InverseProperty] thuộc tính trong khi 'loay hoay' - đó dường như không tạo sự khác biệt
Tôi cũng đã cố gắng thêm tên cột một cách rõ ràng như đã đề cập here
Tôi không chắc còn phải thử cái gì khác - nó chỉ bắt đầu xảy ra khi tôi xác định mối quan hệ với Phiên trong FluidUI - đề xuất?
nếu bạn thêm '.ForSqlServerHasConstraintName ("EventId") 'sau' .HasForeignKey (s => s.EventId)' nó thay đổi bất cứ điều gì? – Thomas
vẫn tạo tên cột không hợp lệ sau khi thêm ràng buộc - được kiểm tra bằng cách thêm vào cả hai mối quan hệ với Nhãn hiệu và Phiên – Danish
Âm thanh ngu ngốc nhưng tôi phải đối mặt với vấn đề này sau khi Resharper tái cấu trúc tên bảng và tôi đã không nhận thấy nó. Đổi tên 'ProductCategory' thành' ProductCategories' và Resharper được cấu trúc lại '.ToTable (" ProductCategory ")' thành '.ToTable (" ProductCategories ")' – guneysus