2013-10-08 80 views
0

我在這篇文章中提出過一個問題:實體框架5無效的列名?,但由於代碼量大,很難檢測到問題的根源。所以,我在另一個簡單的環境中再現了異常,希望有人幫助我解決這個問題:實體框架5代碼第一個無效列名?

在這個簡單的應用程序中,我使用一個帶有2個按鈕的wpf窗口,用於關閉窗口,另一個用於添加模擬

我把它填入代碼中。

簡而言之,該異常的消息是「無效的列名稱simulation_SimulationID」。

代碼如下:兩個表的

腳本

USE [dblease] 
GO 

/****** Object: Table [dbo].[Echeance] Script Date: 10/07/2013 14:54:56 ******/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

CREATE TABLE [dbo].[Echeance](
[EheanceId] [bigint] IDENTITY(1,1) NOT NULL, 
[EchNumber] [tinyint] NOT NULL, 
[date] [date] NOT NULL, 
[principal] [float] NULL, 
[interet] [float] NULL, 
[isLoy1] [tinyint] NOT NULL, 
[SimulationId] [bigint] NOT NULL, 
CONSTRAINT [PK_Echeance] PRIMARY KEY CLUSTERED 
(
[EheanceId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

ALTER TABLE [dbo].[Echeance] WITH CHECK ADD CONSTRAINT [FK_Echeance_simulation]  FOREIGN KEY([SimulationId]) REFERENCES [dbo].[simulation] ([SimulationId]) ON DELETE CASCADE 
GO 

ALTER TABLE [dbo].[Echeance] CHECK CONSTRAINT [FK_Echeance_simulation] 
GO 

*************************************************************************** 

USE [dblease] 
GO 

/****** Object: Table [dbo].[simulation] Script Date: 10/07/2013 14:56:32 ******/ 
SET ANSI_NULLS ON 
GO 

    SET QUOTED_IDENTIFIER ON 
GO 

CREATE TABLE [dbo].[simulation](
[SimulationId] [bigint] IDENTITY(1,1) NOT NULL, 
[baseLocative] [float] NULL, 
[txInt] [float] NULL, 
[txVr] [float] NULL, 
[Autofin] [float] NULL, 
[echjour] [tinyint] NOT NULL, 
[duree] [tinyint] NOT NULL, 
[echBegin] [date] NOT NULL, 
[loy1] [float] NULL, 
[loy1_freq] [tinyint] NULL, 
[encfin] [float] NULL, 
[loyer] [float] NULL, 
[txtva] [float] NULL, 
[perio] [tinyint] NOT NULL, 
[ratio] [numeric](9, 3) NULL, 
CONSTRAINT [PK_simulation] PRIMARY KEY CLUSTERED 
([SimulationId] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

WPF窗口(從SQL Server 2008產生)

namespace WpfApplication2 
{ 
    /// <summary> 
    /// Logique d'interaction pour MainWindow.xaml 
    /// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     this.Close(); 
    } 

    private void Button_Click2(object sender, RoutedEventArgs e) 
    { 

     simulation s = new simulation(); 
     s.baseLocative = 18500; 
     s.JourEcheance = 25; 
     DateTime dd; 
     DateTime.TryParse("25/01/2012", out dd); 
     s.BeginEch = dd; 
     int m = 01; 
     int y = 2012; 
     s.duree = 36; 
     for (byte i=0;i < 36; i++) 
     { 
      Echeance ech = new Echeance(); 
      ech.EchNumber = (byte)(i+1); 
      ech.DateEcheance = dd; 
      ech.MontantPrincipal = 500; 
      ech.MontantInteret = 100; 
      ech.MontantHT = 600; 
      ech.MontantTVA = 600 * 0.18; 
      ech.MontantTTC = ech.MontantHT + ech.MontantTVA; 
      s.echeancier.Add(ech); 
      m++; 
      if (m == 13) 
      { 
       m = 1; 
       y = y + 1; 
      } 

      DateTime.TryParse("25/" + m.ToString() + "/" + y.ToString(),out dd); 
     } 

     using (MyContext context = new MyContext()) 
     { 
      try 
      { 
       context.Simulations.Add(s); 
       context.SaveChanges(); 
      } 

      catch (DbUpdateException ex1) 
      { 
       var innerEx = ex1.InnerException; 
       while (innerEx.InnerException != null) 
        innerEx = innerEx.InnerException; 
        MessageBox.Show(innerEx.Message, "DbUpdateException", MessageBoxButton.OK); 
      //throw new Exception(innerEx.Message); 
      } 

      catch (DbEntityValidationException ex3) 
      { 
       var sb = new StringBuilder(); 
       foreach (var entry in ex3.EntityValidationErrors) 
       { 
        foreach (var error in entry.ValidationErrors) 
        { 
         sb.AppendLine(string.Format("{0}-{1}-{2}", 
         entry.Entry.Entity, 
         error.PropertyName, 
         error.ErrorMessage 
        )); 
        } 
       } 
       MessageBox.Show(sb.ToString(),"DbEntityValidationException",MessageBoxButton.OK); 
      //throw new Exception(sb.ToString()); 
     } 

      } 


    } 
} 
} 

語境:

public class MyContext : DbContext 
{ 



    public DbSet<simulation> Simulations { get; set; } 
    public DbSet<Echeance> Echeances { get; set; } 


    public MyContext(string cnxString) 
     : base(cnxString) 
    { 
     // Database.SetInitializer<MyContext>(null); 
    } 


    public MyContext() 
     : base("name=dbleaseEntities") 
    { 
     // Database.SetInitializer<MyContext>(null); 

    } 


    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 

     try 
     { 
      modelBuilder.Configurations.Add(new SimulationMapping()); 
      modelBuilder.Configurations.Add(new EcheanceMapping()); 



      modelBuilder.Entity<simulation>().Ignore(t => t.TotalPri); 
      modelBuilder.Entity<simulation>().Ignore(t => t.TotalInt); 
      modelBuilder.Entity<simulation>().Ignore(t => t.TotalHt); 
      modelBuilder.Entity<simulation>().Ignore(t => t.TotalTva); 
      modelBuilder.Entity<simulation>().Ignore(t => t.TotalTtc); 
      modelBuilder.Entity<simulation>().Ignore(t => t.Montant); 
      modelBuilder.Entity<simulation>().Ignore(t => t.Txtint); 
      modelBuilder.Entity<simulation>().Ignore(t => t.Txtvr); 
      modelBuilder.Entity<simulation>().Ignore(t => t.Autofinancement); 
      modelBuilder.Entity<simulation>().Ignore(t => t.JourEcheance); 
      modelBuilder.Entity<simulation>().Ignore(t => t.DureeCtr); 
      modelBuilder.Entity<simulation>().Ignore(t => t.BeginEch); 
      modelBuilder.Entity<simulation>().Ignore(t => t.Loyer); 
      modelBuilder.Entity<simulation>().Ignore(t => t.Periodicite); 
      modelBuilder.Entity<simulation>().Ignore(t => t.FirstLoyer); 
      modelBuilder.Entity<simulation>().Ignore(t => t.TxtTva); 
      modelBuilder.Entity<simulation>().Ignore(t => t.FrequenceLoy1); 
      modelBuilder.Entity<simulation>().Ignore(t => t.LoyForDuplicate); 
      modelBuilder.Entity<simulation>().Ignore(t => t.Ratio); 
      modelBuilder.Entity<simulation>().Ignore(t => t.Timbre); 
      modelBuilder.Entity<simulation>().Ignore(t => t.Encfin); 


      modelBuilder.Entity<Echeance>().Ignore(t => t.NumLoy); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.DateEcheance); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.MontantPrincipal); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.MontantInteret); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.MontantHT); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.MontantTVA); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.MontantTTC); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.FirstLoyer); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.Encours); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.Amortfin); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.TauxTVA); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.tva); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.txtva); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.horstaxe); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.ttc); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.amtfin); 
      modelBuilder.Entity<Echeance>().Ignore(t => t.encfin); 

      base.OnModelCreating(modelBuilder); 

     } 
     catch (Exception ex) 
     { 

      throw ex; 

     } 

    } 


} 

Entitie s:

//simulation 
public partial class simulation : INotifyPropertyChanged 
{ 

    public long SimulationId { get; set; } 
    public Double baseLocative { get; set; } 
    public Double txInt { get; set; } 
    public Double txVr { get; set; } 
    public Double Autofin { get; set; } 
    public byte echjour { get; set; } 
    public Byte duree { get; set; } 
    public System.DateTime echBegin { get; set; } 
    public Double loy1 { get; set; } 
    public Byte loy1_freq { get; set; } 
    public Double encfin { get; set; } 
    public Double loyer { get; set; } 
    public Double txtva { get; set; } 
    public Byte perio { get; set; } 
    public Double ratio { get; set; } 

    public virtual IList<Echeance> echeancier { get; set; } 
    //public virtual ICollection<Echeance> echeancier { get; set; } 

    //Non mappés 

    Double Totpri; 
    Double Totint; 
    Double Totht; 
    Double Tottva; 
    Double Totttc; 
    Double loyDuplicate; 
    Double _timbre; 


    //Constructeur sans paramètres 
    public simulation() 
    { 
     baseLocative = 0.000; 
     txInt = 0.000; 
     txVr = 0.000; 
     Autofin = 0.000; 
     echjour = 25; 
     duree = 36; 
     loyer = 0.000; 
     loy1 = 0.000; 
     loy1_freq = 1; 
     perio = 1; 
     txtva = 18.000; 
     Totpri = 0.000; 
     Totint = 0.000; 
     Totht = 0.000; 
     Tottva = 0.000; 
     Totttc = 0.000; 
     loyDuplicate = 0.000; 
     _timbre = 0.000; 

     int y = DateTime.Today.Year; 
     int m = DateTime.Today.Month; 
     int djour = DateTime.Today.Day; 
     int j = 25; 

     if (djour > 25) 
     { 

      m = m + 1; 
      if (m > 12) 
      { 
       m = 1; 
       y = y + 1; 
      } 
     } 



     //Date française 
     CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR"); 
     DateTimeStyles styles = DateTimeStyles.None; 
     DateTime dd; 

     string datestring = j.ToString() + "/" + m.ToString() + "/" + y.ToString(); 
     DateTime.TryParse(datestring, culture, styles, out dd); 
     echBegin = dd; 

     echeancier = new List<Echeance>(); 
    } 


    //Constructeur avec paramètres 
    public simulation(Double bl, Double txi, Double txr, Double autofin, Byte jouech, Byte ldur, DateTime ech1, Double loy1mnt, Byte per, Double timbre) 
    { 
     baseLocative = bl; 
     txInt = txi; 
     txVr = txr; 
     perio = per; 
     Autofin = autofin; 
     echjour = jouech; 
     duree = ldur; 
     echBegin = ech1; 
     loy1 = loy1mnt; 
     loy1_freq = 1; 
     encfin = 0.000; 
     ratio = 0.000; 
     _timbre = timbre; 
     echeancier = new List<Echeance>(); 
    } 


    //Propriété base locative 
    public Double Montant 
    { 
     get { return baseLocative; } 
     set 
     { 
      baseLocative = value; 
      OnPropertyChanged("Montant"); 
     } 

    } 

    //Propriété Taux d'intérêts 
    public Double Txtint 
    { 
     get { return txInt; } 
     set 
     { 
      txInt = value; 
      OnPropertyChanged("Txtint"); 
     } 

    } 


    //Propriété Taux valeur résiduelle 
    public Double Txtvr 
    { 
     get { return txVr; } 
     set 
     { 
      txVr = value; 
      OnPropertyChanged("Txtvr"); 
     } 

    } 


    //Propriété Autofinancement 
    public Double Autofinancement 
    { 
     get { return Autofin; } 
     set 
     { 
      Autofin = value; 
      OnPropertyChanged("Autofinancement"); 
     } 

    } 

    //Propriété Jour échéance 
    public Byte JourEcheance 
    { 
     get { return echjour; } 
     set 
     { 
      echjour = value; 
      OnPropertyChanged("JourEcheance"); 
     } 

    } 

    //Propriété Jour échéance 
    public Byte DureeCtr 
    { 
     get { return duree; } 
     set 
     { 
      duree = value; 
      OnPropertyChanged("DureeCtr"); 
     } 

    } 

    //Propriété Jour échéance 
    public DateTime BeginEch 
    { 
     get { return echBegin; } 
     set 
     { 
      echBegin = value; 
      OnPropertyChanged("BeginEch"); 
     } 

    } 

    //Propriété Jour échéance 
    public Double Loyer 
    { 
     get { return loyer; } 
     set 
     { 
      loyer = value; 
      OnPropertyChanged("Loyer"); 
     } 
    } 

    //Périodicté 
    public Byte Periodicite 
    { 
     get { return perio; } 
     set 
     { 
      perio = value; 
      OnPropertyChanged("Periodicite"); 
     } 
    } 



    //Premier loyer 
    public Double FirstLoyer 
    { 
     get { return loy1; } 
     set 
     { 
      loy1 = value; 
      OnPropertyChanged("FirstLoyer"); 
     } 
    } 

    public Double TxtTva 
    { 
     get { return txtva; } 
     set 
     { 
      txtva = value; 
      OnPropertyChanged("TxtTva"); 
     } 
    } 


    //Nombre de répétition du premier loyer 
    public Byte FrequenceLoy1 
    { 
     get { return loy1_freq; } 
     set 
     { 
      loy1_freq = value; 
      OnPropertyChanged("FrequenceLoy1"); 
     } 
    } 


    public Double TotalPri 
    { 
     get { return Totpri; } 
     set 
     { 
      Totpri = value; 
      OnPropertyChanged("TotalPri"); 
     } 
    } 

    public Double TotalInt 
    { 
     get { return Totint; } 
     set 
     { 
      Totint = value; 
      OnPropertyChanged("TotalInt"); 
     } 
    } 

    public Double TotalHt 
    { 
     get { return Totht; } 
     set 
     { 
      Totht = value; 
      OnPropertyChanged("TotalHt"); 
     } 
    } 



    public Double TotalTva 
    { 
     get { return Tottva; } 
     set 
     { 
      Tottva = value; 
      OnPropertyChanged("TotalTva"); 
     } 
    } 

    public Double TotalTtc 
    { 
     get { return Totttc; } 
     set 
     { 
      Totttc = value; 
      OnPropertyChanged("TotalTtc"); 
     } 
    } 


    public Double Encfin 
    { 
     get { return encfin; } 
     set 
     { 
      encfin = value; 
      OnPropertyChanged("Encfin"); 
     } 
    } 

    public Double Ratio 
    { 
     get { return ratio; } 
     set 
     { 
      ratio = value; 
      OnPropertyChanged("Ratio"); 
     } 
    } 


    public Double LoyForDuplicate 
    { 
     get 
     { 
      return loyDuplicate; 
     } 
     set 
     { 
      loyDuplicate = value; 
      OnPropertyChanged("LoyForDuplicate"); 
     } 
    } 


    //public IList<Echeance> ListEcheances 
    public IList<Echeance> ListEcheances 
    { 
     get 
     { 
      return echeancier; 
     } 
     set 
     { 
      echeancier = value; 
      OnPropertyChanged("ListEcheance"); 
     } 
    } 


    public Double Timbre 
    { 
     get 
     { 
      return _timbre; 
     } 
     set 
     { 
      _timbre = value; 
      OnPropertyChanged("Timbre"); 
     } 
    } 


    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

//Echeance 
public partial class Echeance : INotifyPropertyChanged 
{ 


    public long EheanceId { get; set; } 
    public byte EchNumber { get; set; } 
    public System.DateTime date { get; set; } 
    public Double principal { get; set; } 
    public Double interet { get; set; } 
    public byte isLoy1 { get; set; } 
    public long SimulationId { get; set; } 
    public virtual simulation simulation { get; set; } 

    //Attributs non mappés 
    public Double tva { get; set; } 
    public Double txtva { get; set; } 
    public Double horstaxe { get; set; } 
    public Double ttc { get; set; } 
    public Double amtfin { get; set; } 
    public Double encfin { get; set; } 

    //Constructeur avec numéro de loyer 
    public Echeance(byte nnloy) 
    { 
     EchNumber = nnloy; 
     date = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 25); 
     principal = 0.000; 
     interet = 0.000; 
     horstaxe = 0.000; 
     tva = 0.000; 
     ttc = 0.000; 
     isLoy1 = 0; 
     ttc = 0.000; 
     amtfin = 0.000; 
     encfin = 0.000; 
    } 



    //Constructeur sans paramètres avec valeurs par défaut 
    public Echeance() 
    { 
     EchNumber = 0; 
     date = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 25); 
     principal = 0.000; 
     interet = 0.000; 
     horstaxe = 0.000; 
     tva = 0.000; 
     ttc = 0.000; 
     isLoy1 = 0; 
     ttc = 0.000; 
     amtfin = 0.000; 
     encfin = 0.000; 
    } 



    //Propriété Numloy : numéro loyer 
    public byte NumLoy 
    { 
     get { return EchNumber; } 
     set 
     { 
      EchNumber = value; 
      OnPropertyChanged("Numloy"); 
     } 
    } 

    public DateTime DateEcheance 
    { 
     get { return date; } 
     set 
     { 
      date = value; 
      OnPropertyChanged("DateEcheance"); 
     } 

    } 

    public Double MontantPrincipal 
    { 
     get 
     { 
      return principal; 
     } 

     set 
     { 
      principal = value; 
      OnPropertyChanged("MontantPrincipal"); 
     } 
    } 


    public Double MontantInteret 
    { 
     get 
     { 
      return interet; 
     } 

     set 
     { 
      interet = value; 
      OnPropertyChanged("MontantInteret"); 
     } 
    } 

    public Double MontantHT 
    { 
     get 
     { 
      return horstaxe; 
     } 

     set 
     { 
      horstaxe = value; 
      OnPropertyChanged("MontantHT"); 
     } 
    } 


    public Double MontantTVA 
    { 
     get 
     { 
      return tva; 
     } 

     set 
     { 
      tva = value; 
      OnPropertyChanged("MontantTVA"); 
     } 
    } 

    public Double MontantTTC 
    { 
     get 
     { 
      return ttc; 
     } 

     set 
     { 
      ttc = value; 
      OnPropertyChanged("MontantTTC"); 
     } 
    } 


    public byte FirstLoyer 
    { 
     get { return isLoy1; } 
     set 
     { 
      isLoy1 = value; 
      OnPropertyChanged("FirstLoyer"); 
     } 
    } 

    public Double Encours 
    { 
     get { return encfin; } 
     set 
     { 
      encfin = value; 
      OnPropertyChanged("Encours"); 
     } 
    } 


    public Double Amortfin 
    { 
     get { return amtfin; } 
     set 
     { 
      amtfin = value; 
      OnPropertyChanged("Amortfin"); 
     } 
    } 


    public Double TauxTVA 
    { 
     get { return txtva; } 
     set 
     { 
      txtva = value; 
      OnPropertyChanged("TauxTVA"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 

     if (handler != null) 
      handler(this, new PropertyChangedEventArgs(name)); 

    } 

} 

回答

0

我找到了答案here

尖端是:如果我爲WPF數據綁定在數據網格

blic IList<Echeance> ListEcheances 
{ 
    get 
    { 
     return echeancier; 
    } 
    set 
    { 
     echeancier = value; 
     OnPropertyChanged("ListEcheances"); 
    } 
} 

屬性echeancier被mentionned兩次:

1:對於實體聲明

public virtual IList<Echeance> echeancier { get; set; } 

2刪除第二個聲明,它工作正常。