2013-02-23 163 views
2

在我.NET(3.5)應用與EF我用下面的代碼中插入新的記錄,但ADDOBJECT()不工作。ADDOBJECT不工作

我很驚訝,因爲它發生在第一次,甚至在EF工作之前我曾經工作過。以下錯誤tbAirline

public partial class GsecEntities : global::System.Data.Objects.ObjectContext 
{ 
    /// <summary> 
    /// Initializes a new GsecEntities object using the connection string found in the 'GsecEntities' section of the application configuration file. 
    /// </summary> 
    public GsecEntities() : 
      base("name=GsecEntities", "GsecEntities") 
    { 
     this.OnContextCreated(); 
    } 
    /// <summary> 
    /// Initialize a new GsecEntities object. 
    /// </summary> 
    public GsecEntities(string connectionString) : 
      base(connectionString, "GsecEntities") 
    { 
     this.OnContextCreated(); 
    } 
    /// <summary> 
    /// Initialize a new GsecEntities object. 
    /// </summary> 
    public GsecEntities(global::System.Data.EntityClient.EntityConnection connection) : 
      base(connection, "GsecEntities") 
    { 
     this.OnContextCreated(); 
    } 
    partial void OnContextCreated(); 
    /// <summary> 
    /// There are no comments for tbAirLines in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public global::System.Data.Objects.ObjectQuery<tbAirLine> tbAirLines 
    { 
     get 
     { 
      if ((this._tbAirLines == null)) 
      { 
       this._tbAirLines = base.CreateQuery<tbAirLine>("[tbAirLines]"); 
      } 
      return this._tbAirLines; 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private global::System.Data.Objects.ObjectQuery<tbAirLine> _tbAirLines; 
    /// <summary> 
    /// There are no comments for tbBanks in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public global::System.Data.Objects.ObjectQuery<tbBank> tbBanks 
    { 
     get 
     { 
      if ((this._tbBanks == null)) 
      { 
       this._tbBanks = base.CreateQuery<tbBank>("[tbBanks]"); 
      } 
      return this._tbBanks; 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private global::System.Data.Objects.ObjectQuery<tbBank> _tbBanks; 
    /// <summary> 
    /// There are no comments for tbCities in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public global::System.Data.Objects.ObjectQuery<tbCity> tbCities 
    { 
     get 
     { 
      if ((this._tbCities == null)) 
      { 
       this._tbCities = base.CreateQuery<tbCity>("[tbCities]"); 
      } 
      return this._tbCities; 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private global::System.Data.Objects.ObjectQuery<tbCity> _tbCities; 
    /// <summary> 
    /// There are no comments for tbCommodities in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public global::System.Data.Objects.ObjectQuery<tbCommodity> tbCommodities 
    { 
     get 
     { 
      if ((this._tbCommodities == null)) 
      { 
       this._tbCommodities = base.CreateQuery<tbCommodity>("[tbCommodities]"); 
      } 
      return this._tbCommodities; 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private global::System.Data.Objects.ObjectQuery<tbCommodity> _tbCommodities; 
    /// <summary> 
    /// There are no comments for tbCountries in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public global::System.Data.Objects.ObjectQuery<tbCountry> tbCountries 
    { 
     get 
     { 
      if ((this._tbCountries == null)) 
      { 
       this._tbCountries = base.CreateQuery<tbCountry>("[tbCountries]"); 
      } 
      return this._tbCountries; 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private global::System.Data.Objects.ObjectQuery<tbCountry> _tbCountries; 
    /// <summary> 
    /// There are no comments for tbFlights in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public global::System.Data.Objects.ObjectQuery<tbFlight> tbFlights 
    { 
     get 
     { 
      if ((this._tbFlights == null)) 
      { 
       this._tbFlights = base.CreateQuery<tbFlight>("[tbFlights]"); 
      } 
      return this._tbFlights; 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private global::System.Data.Objects.ObjectQuery<tbFlight> _tbFlights; 
    /// <summary> 
    /// There are no comments for tbGrpCommodities in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public global::System.Data.Objects.ObjectQuery<tbGrpCommodity> tbGrpCommodities 
    { 
     get 
     { 
      if ((this._tbGrpCommodities == null)) 
      { 
       this._tbGrpCommodities = base.CreateQuery<tbGrpCommodity>("[tbGrpCommodities]"); 
      } 
      return this._tbGrpCommodities; 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private global::System.Data.Objects.ObjectQuery<tbGrpCommodity> _tbGrpCommodities; 
    /// <summary> 
    /// There are no comments for tbPersons in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public global::System.Data.Objects.ObjectQuery<tbPerson> tbPersons 
    { 
     get 
     { 
      if ((this._tbPersons == null)) 
      { 
       this._tbPersons = base.CreateQuery<tbPerson>("[tbPersons]"); 
      } 
      return this._tbPersons; 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private global::System.Data.Objects.ObjectQuery<tbPerson> _tbPersons; 
    /// <summary> 
    /// There are no comments for tbPersonTypes in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public global::System.Data.Objects.ObjectQuery<tbPersonType> tbPersonTypes 
    { 
     get 
     { 
      if ((this._tbPersonTypes == null)) 
      { 
       this._tbPersonTypes = base.CreateQuery<tbPersonType>("[tbPersonTypes]"); 
      } 
      return this._tbPersonTypes; 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private global::System.Data.Objects.ObjectQuery<tbPersonType> _tbPersonTypes; 
    /// <summary> 
    /// There are no comments for tbAirLines in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public void AddTotbAirLines(tbAirLine tbAirLine) 
    { 
     base.AddObject("tbAirLines", tbAirLine); 
    } 
    /// <summary> 
    /// There are no comments for tbBanks in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public void AddTotbBanks(tbBank tbBank) 
    { 
     base.AddObject("tbBanks", tbBank); 
    } 
    /// <summary> 
    /// There are no comments for tbCities in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public void AddTotbCities(tbCity tbCity) 
    { 
     base.AddObject("tbCities", tbCity); 
    } 
    /// <summary> 
    /// There are no comments for tbCommodities in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public void AddTotbCommodities(tbCommodity tbCommodity) 
    { 
     base.AddObject("tbCommodities", tbCommodity); 
    } 
    /// <summary> 
    /// There are no comments for tbCountries in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public void AddTotbCountries(tbCountry tbCountry) 
    { 
     base.AddObject("tbCountries", tbCountry); 
    } 
    /// <summary> 
    /// There are no comments for tbFlights in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public void AddTotbFlights(tbFlight tbFlight) 
    { 
     base.AddObject("tbFlights", tbFlight); 
    } 
    /// <summary> 
    /// There are no comments for tbGrpCommodities in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public void AddTotbGrpCommodities(tbGrpCommodity tbGrpCommodity) 
    { 
     base.AddObject("tbGrpCommodities", tbGrpCommodity); 
    } 
    /// <summary> 
    /// There are no comments for tbPersons in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public void AddTotbPersons(tbPerson tbPerson) 
    { 
     base.AddObject("tbPersons", tbPerson); 
    } 
    /// <summary> 
    /// There are no comments for tbPersonTypes in the schema. 
    /// </summary> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public void AddTotbPersonTypes(tbPersonType tbPersonType) 
    { 
     base.AddObject("tbPersonTypes", tbPersonType); 
    } 
} 
/// <summary> 
/// There are no comments for GsecModel.tbAirLine in the schema. 
/// </summary> 
/// <KeyProperties> 
/// Id 
/// </KeyProperties> 
[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="GsecModel", Name="tbAirLine")] 
[global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)] 
[global::System.Serializable()] 
public partial class tbAirLine : global::System.Data.Objects.DataClasses.EntityObject 
{ 
    /// <summary> 
    /// Create a new tbAirLine object. 
    /// </summary> 
    /// <param name="id">Initial value of Id.</param> 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public static tbAirLine CreatetbAirLine(long id) 
    { 
     tbAirLine tbAirLine = new tbAirLine(); 
     tbAirLine.Id = id; 
     return tbAirLine; 
    } 
    /// <summary> 
    /// There are no comments for property Id in the schema. 
    /// </summary> 
    [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 
    [global::System.Runtime.Serialization.DataMemberAttribute()] 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public long Id 
    { 
     get 
     { 
      return this._Id; 
     } 
     set 
     { 
      this.OnIdChanging(value); 
      this.ReportPropertyChanging("Id"); 
      this._Id = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value); 
      this.ReportPropertyChanged("Id"); 
      this.OnIdChanged(); 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private long _Id; 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    partial void OnIdChanging(long value); 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    partial void OnIdChanged(); 
    /// <summary> 
    /// There are no comments for property Name in the schema. 
    /// </summary> 
    [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()] 
    [global::System.Runtime.Serialization.DataMemberAttribute()] 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public string Name 
    { 
     get 
     { 
      return this._Name; 
     } 
     set 
     { 
      this.OnNameChanging(value); 
      this.ReportPropertyChanging("Name"); 
      this._Name = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true); 
      this.ReportPropertyChanged("Name"); 
      this.OnNameChanged(); 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private string _Name; 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    partial void OnNameChanging(string value); 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    partial void OnNameChanged(); 
    /// <summary> 
    /// There are no comments for property ShortName in the schema. 
    /// </summary> 
    [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()] 
    [global::System.Runtime.Serialization.DataMemberAttribute()] 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public string ShortName 
    { 
     get 
     { 
      return this._ShortName; 
     } 
     set 
     { 
      this.OnShortNameChanging(value); 
      this.ReportPropertyChanging("ShortName"); 
      this._ShortName = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true); 
      this.ReportPropertyChanged("ShortName"); 
      this.OnShortNameChanged(); 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private string _ShortName; 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    partial void OnShortNameChanging(string value); 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    partial void OnShortNameChanged(); 
    /// <summary> 
    /// There are no comments for property Remark in the schema. 
    /// </summary> 
    [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()] 
    [global::System.Runtime.Serialization.DataMemberAttribute()] 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    public string Remark 
    { 
     get 
     { 
      return this._Remark; 
     } 
     set 
     { 
      this.OnRemarkChanging(value); 
      this.ReportPropertyChanging("Remark"); 
      this._Remark = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true); 
      this.ReportPropertyChanged("Remark"); 
      this.OnRemarkChanged(); 
     } 
    } 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    private string _Remark; 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    partial void OnRemarkChanging(string value); 
    [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
    partial void OnRemarkChanged(); 
} 

而其給出

我的代碼

public void NewAirline(string name, string sname, string remark) 
    { 
     GsecEntities e = new GsecEntities(); 
     tbAirLine al = new tbAirLine() 
     { 
      Name = name, 
      Remark = remark, 
      ShortName = sname 
     }; 
     e.tbAirLines.AddObject(al); 
     e.SaveChanges(); 
    } 

至於建議,我在這裏寫代碼

錯誤1「System.Data.Objects .ObjectQuery'不包含'AddObject'的定義,也沒有擴展方法'AddObject'接受類型「System.Data.Objects.ObjectQuery」的第一個參數可以找到(是否缺少using指令或程序集引用?)

+0

請發帖執行tbAirLine.AddObject() – 2013-02-23 07:41:08

+0

看來我也很震驚。因爲我知道你的這段代碼沒有任何問題。正如我認爲它應該完美... – 2013-02-23 07:41:33

+0

那麼錯誤是什麼? – spajce 2013-02-23 07:43:32

回答

3

.NET 3.5版暴露在.NET 4.0 ObjectQuery<T>ObjectSet<T>。因此,您需要使用e.AddTotbAirLines(tbAirLine)e.AddObject("tbAirLines", tbAirLine)。兩者都可以在提交的生成代碼的第185行中找到。在.NET 4.0中,他們決定實施它,就像你在問題中展示的一樣。

+0

現在**無法加載指定的元數據資源**錯誤出現.... – 2013-02-23 08:48:32

+0

在這種情況下,你可能想看看[這裏](http://stackoverflow.com/問題/ 689355/metadataexception-unable-load-the-specified-metadata-resource)進行故障排除。 – Caramiriel 2013-02-23 08:51:32

+0

感謝您的好友,您的解決方案有效。如果你靠近我,我會給你一隻熊......並且+1給你一個很好的答案...... – 2013-02-23 08:55:06