2014-01-23 167 views
7

即時得到這使得完全沒有意義考慮所有屬性都是簡單類型,也沒有FeeLevel也不在此對象LevelId對象錯誤「無效的列名稱FeeLevel_LevelId「EF代碼優先 - 無效的列名

左右。我的背景是:

public partial class FeesDbContext : DisconnectedEntityContext 
    { 

     public DbSet<Currency> Currencies { get; set; } 

     public DbSet<FeeLevel> FeeLevels { get; set; } 

     public DbSet<FeeLevelDetail> FeeLevelDetails { get; set; } 

     public DbSet<FeeType> FeeTypes { get; set; } 

     public DbSet<MemberFeeDiscountLevel> MemberFeeDiscountLevels { get; set; } 

     public FeesDbContext() 
      : base("FeesDb") { } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Configurations.Add(new FeeLevelMap()); 
      modelBuilder.Configurations.Add(new FeeLevelDetailMap()); 
      modelBuilder.Configurations.Add(new FeeTypeMap()); 
      modelBuilder.Configurations.Add(new CurrencyMap()); 
      modelBuilder.Configurations.Add(new MemberFeeDiscountLevelMap()); 
     } 

     public static void ApplyChanges<TEntity>(TEntity root) 
        where TEntity : class, IObjectWithState 
     { 
      using (var context = new FeesDbContext()) 
      { 
       context.Set<TEntity>().Add(root); 
       foreach (var entry in context.ChangeTracker.Entries<IObjectWithState>()) 
       { 
        IObjectWithState stateInfo = entry.Entity; 
        entry.State = ConvertState(stateInfo.State); 
       } 
       context.SaveChanges(); 
      } 
     } 
} 

的POCO是:

public partial class MemberFeeDiscountLevel : AbstractState 
    { 

     public long MemberFeeDiscountLevelId { get; set; } 
     public System.Guid MemberId { get; set; } 
     public short MemberAsType { get; set; } 
     public long FeeDiscountLevelId { get; set; } 
     public System.DateTime FeeDiscountLevelAppliedDate { get; set; } 
     public Nullable<System.DateTime> FeeDiscountLevelExpiresDate { get; set; } 
     public Nullable<long> FallbackFeeDiscountLevelId { get; set; } 
     public System.Guid UserId { get; set; } 
     public System.DateTime LastModified { get; set; } 


     public MemberFeeDiscountLevel(ObjectState state) : base(state) { } 

     public MemberFeeDiscountLevel() 
     { 
     } 
    } 

它的映射:

public class MemberFeeDiscountLevelMap : EntityTypeConfiguration<MemberFeeDiscountLevel> 
    { 
     public MemberFeeDiscountLevelMap() 
     { 
      // Primary Key 
      this.HasKey(t => t.MemberFeeDiscountLevelId); 

      this.Ignore(t => t.State); 

      // Properties 
      // Table & Column Mappings 
      this.ToTable("MemberFeeDiscountLevel"); 
      this.Property(t => t.MemberFeeDiscountLevelId).HasColumnName("MemberFeeDiscountLevelId"); 
      this.Property(t => t.MemberId).HasColumnName("MemberId"); 
      this.Property(t => t.MemberAsType).HasColumnName("MemberAsType"); 
      this.Property(t => t.FeeDiscountLevelId).HasColumnName("FeeDiscountLevelId"); 
      this.Property(t => t.FeeDiscountLevelAppliedDate).HasColumnName("FeeDiscountLevelAppliedDate"); 
      this.Property(t => t.FeeDiscountLevelExpiresDate).HasColumnName("FeeDiscountLevelExpiresDate"); 
      this.Property(t => t.FallbackFeeDiscountLevelId).HasColumnName("FallbackFeeDiscountLevelId"); 
      this.Property(t => t.UserId).HasColumnName("UserId"); 
      this.Property(t => t.LastModified).HasColumnName("LastModified"); 
     } 
    } 

數據庫表:

enter image description here

現在也沒有關係。然而,EF在生成的SQL語句:

exec sp_executesql N'INSERT [dbo].[MemberFeeDiscountLevel]([MemberId], [MemberAsType], [FeeDiscountLevelId], [FeeDiscountLevelAppliedDate], [FeeDiscountLevelExpiresDate], [FallbackFeeDiscountLevelId], [UserId], [LastModified], [FeeLevel_LevelId]) 
VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, NULL) 
SELECT [MemberFeeDiscountLevelId] 
FROM [dbo].[MemberFeeDiscountLevel] 
WHERE @@ROWCOUNT > 0 AND [MemberFeeDiscountLevelId] = scope_identity()',N'@0 uniqueidentifier,@1 smallint,@2 bigint,@3 datetime2(7),@4 datetime2(7),@5 bigint,@6 uniqueidentifier,@7 datetime2(7),@8 int',@0='DAF771D1-079F-4743-B5C7-FD0FA1C63E19',@1=0,@2=1012,@3='2014-01-24 12:05:36.0608347',@4='2014-02-01 00:00:00',@5=1018,@6='EEDF2C83-2123-4B1C-BF8D-BE2D2FA26D09',@7='2014-01-24 12:05:36.0608347' 
go 

UPDATE:

創建一個新的Fees2DbContext剝出其他DbSets「修復」的問題....但我不知道爲什麼......沒有這些類別/集合與所討論的類別有關。

public partial class Fees2DbContext : DisconnectedEntityContext 
    { 
     public DbSet<MemberFeeDiscountLevel> MemberFeeDiscountLevels { get; set; } 

     public Fees2DbContext() 
      : base("FeesDb") { } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Configurations.Add(new MemberFeeDiscountLevelMap()); 
     } 

     public static void ApplyChanges<TEntity>(TEntity root) 
        where TEntity : class, IObjectWithState 
     { 
      using (var context = new Fees2DbContext()) 
      { 
       context.Set<TEntity>().Add(root); 
       foreach (var entry in context.ChangeTracker.Entries<IObjectWithState>()) 
       { 
        IObjectWithState stateInfo = entry.Entity; 
        entry.State = ConvertState(stateInfo.State); 
       } 
       context.SaveChanges(); 
      } 
     } 
    } 

更新2:

 public partial class FeeLevel : AbstractState 
     { 
      public FeeLevel() 
      { 
       this.FeeLevelDetails = new List<FeeLevelDetail>(); 
       this.MemberFeeDiscountLevels = new List<MemberFeeDiscountLevel>(); 
      } 

      public long LevelId { get; set; } 

      public string LevelName { get; set; } 

      public int CurrencyId { get; set; } 

      public System.DateTime LastModified { get; set; } 

      public bool IsSystemOwned { get; set; } 

      public System.Guid UserId { get; set; } 

      public virtual Currency Currency { get; set; } 

      [ScriptIgnore] 
      public virtual ICollection<FeeLevelDetail> FeeLevelDetails { get; set; } 

      public virtual ICollection<MemberFeeDiscountLevel> MemberFeeDiscountLevels { get; set; } 
     } 

public class FeeLevelMap : EntityTypeConfiguration<FeeLevel> 
    { 
     public FeeLevelMap() 
     { 
      // Primary Key 
      this.HasKey(t => t.LevelId); 

      this.Ignore(t => t.State); 

      // Properties 
      this.Property(t => t.LevelId); 
      // .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 

      this.Property(t => t.LevelName) 
       .IsRequired() 
       .HasMaxLength(50); 

      // Table & Column Mappings 
      this.ToTable("FeeLevel"); 
      this.Property(t => t.LevelId).HasColumnName("LevelId"); 
      this.Property(t => t.LevelName).HasColumnName("LevelName"); 
      this.Property(t => t.CurrencyId).HasColumnName("CurrencyId"); 
      this.Property(t => t.LastModified).HasColumnName("LastModified"); 
      this.Property(t => t.UserId).HasColumnName("UserId"); 

      // Relationships 
      this.HasRequired(t => t.Currency) 
       .WithMany(t => t.FeeLevels) 
       .HasForeignKey(d => d.CurrencyId); 

     } 
    } 
+0

'FeeLevel'實體是否有'MemberFeeDiscountLevel'的集合? – Slauma

+0

是的,根據您的問題更新2。 EF在這裏做什麼?我沒有明白。 – rism

回答

10

FeeLevel.MemberFeeDiscountLevels是導航屬性和它引入FeeLevelMemberFeeDiscountLevel之間的一對多的關係:一個FeeLevel可以有許多MemberFeeDiscountLevels這意味着在相同的時間一個MemberFeeDiscountLevel有一個單一的FeeLevel。雖然您在MemberFeeDiscountLevel中沒有導航和外鍵屬性,但數據庫必須在MemberFeeDiscountLevel表中具有外鍵才能建立此關係的模型。 EF假定默認的FK名稱爲「相關實體名稱+下劃線+主鍵名稱」= FeeLevel_LevelId。因爲你的數據庫表沒有這個列,你會得到異常。

+0

因爲我一直在使用Entity Framework Power工具來自動生成一些代碼,但很明顯錯過了某處的更新,所以模型已經不同步了。謝謝。 – rism