实体上下文不更新数据库

时间:2012-06-18 00:25:30

标签: c# entity-framework entity

不知怎的,我的实体数据上下文没有更新数据库.. 在.Net框架中,我做了以下内容:

  1. 像这里创建的实体模型:http://msdn.microsoft.com/en-us/library/bb399739(v=vs.90),其中table = entity名为“Level”。

  2. 在此模型的代码新上下文中添加:

    Main_dataBaseEntities context;
    //...
    context = new Main_dataBaseEntities;
    
  3. 使用Path属性添加了“Level”实体的新对象:

    var z = new Level {Path = "zPath"};
    context.Level.AddObject(z);
    
  4. 保存更改:

    context.SaveChanges();
    
  5. 数据库表没有显示任何内容..如何添加新项目?))

    基于数据库的Entity框架创建的My Entity类如下:

    //------------------------------------------------------------------------------
    // <auto-generated>
    //    This code was generated from a template.
    //
    //    Manual changes to this file may cause unexpected behavior in your application.
    //    Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    using System;
    using System.Data.Objects;
    using System.Data.Objects.DataClasses;
    using System.Data.EntityClient;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using System.Runtime.Serialization;
    
    [assembly: EdmSchemaAttribute()]
    #region EDM Relationship Metadata
    
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Terminals_Brokers", "Broker", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Broker), "Terminal", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Terminal), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Clients_Terminals", "Terminal", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Terminal), "Client", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Client), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_ClientsTickers_Clients", "Client", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Client), "ClientTicker", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.ClientTicker), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Risks_Clients", "Client", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Client), "Risk", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Risk), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_ClientsTickers_Tickers", "Ticker", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Ticker), "ClientTicker", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.ClientTicker), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Tickers_Currencies", "Currency", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Currency), "Ticker", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Ticker), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Levels_Strategies", "Strategy", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Strategy), "Level", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Level), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Levels_Tickers", "Ticker", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Ticker), "Level", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Level), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Transactions_Levels", "Level", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Level), "Transaction", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Transaction), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Risks_Strategies", "Strategy", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Strategy), "Risk", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Risk), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Risks_Tickers", "Ticker", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Ticker), "Risk", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Risk), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Transaction_Risk", "Risk", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Risk), "Transaction", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Transaction), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Transactions_Statuses", "Status", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.Status), "Transaction", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Transaction), true)]
    [assembly: EdmRelationshipAttribute("Main_dataBaseModel", "FK_Terminals_TerminalTypes", "TerminalType", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(trust_manager_v_1._0.TerminalType), "Terminal", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(trust_manager_v_1._0.Terminal), true)]
    
    #endregion
    
    namespace trust_manager_v_1._0
    {
        #region Contexts
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public partial class Main_dataBaseEntities : ObjectContext
        {
            #region Constructors
    
            /// <summary>
            /// Initializes a new Main_dataBaseEntities object using the connection string found in the 'Main_dataBaseEntities' section of the application configuration file.
            /// </summary>
            public Main_dataBaseEntities() : base("name=Main_dataBaseEntities", "Main_dataBaseEntities")
            {
                this.ContextOptions.LazyLoadingEnabled = true;
                OnContextCreated();
            }
    
            /// <summary>
            /// Initialize a new Main_dataBaseEntities object.
            /// </summary>
            public Main_dataBaseEntities(string connectionString) : base(connectionString, "Main_dataBaseEntities")
            {
                this.ContextOptions.LazyLoadingEnabled = true;
                OnContextCreated();
            }
    
            /// <summary>
            /// Initialize a new Main_dataBaseEntities object.
            /// </summary>
            public Main_dataBaseEntities(EntityConnection connection) : base(connection, "Main_dataBaseEntities")
            {
                this.ContextOptions.LazyLoadingEnabled = true;
                OnContextCreated();
            }
    
            #endregion
    
            #region Partial Methods
    
            partial void OnContextCreated();
    
            #endregion
    
            #region ObjectSet Properties
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<Broker> Broker
            {
                get
                {
                    if ((_Broker == null))
                    {
                        _Broker = base.CreateObjectSet<Broker>("Broker");
                    }
                    return _Broker;
                }
            }
            private ObjectSet<Broker> _Broker;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<Client> Client
            {
                get
                {
                    if ((_Client == null))
                    {
                        _Client = base.CreateObjectSet<Client>("Client");
                    }
                    return _Client;
                }
            }
            private ObjectSet<Client> _Client;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<ClientTicker> ClientTicker
            {
                get
                {
                    if ((_ClientTicker == null))
                    {
                        _ClientTicker = base.CreateObjectSet<ClientTicker>("ClientTicker");
                    }
                    return _ClientTicker;
                }
            }
            private ObjectSet<ClientTicker> _ClientTicker;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<Currency> Currency
            {
                get
                {
                    if ((_Currency == null))
                    {
                        _Currency = base.CreateObjectSet<Currency>("Currency");
                    }
                    return _Currency;
                }
            }
            private ObjectSet<Currency> _Currency;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<Level> Level
            {
                get
                {
                    if ((_Level == null))
                    {
                        _Level = base.CreateObjectSet<Level>("Level");
                    }
                    return _Level;
                }
            }
            private ObjectSet<Level> _Level;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<Risk> Risk
            {
                get
                {
                    if ((_Risk == null))
                    {
                        _Risk = base.CreateObjectSet<Risk>("Risk");
                    }
                    return _Risk;
                }
            }
            private ObjectSet<Risk> _Risk;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<Status> Status
            {
                get
                {
                    if ((_Status == null))
                    {
                        _Status = base.CreateObjectSet<Status>("Status");
                    }
                    return _Status;
                }
            }
            private ObjectSet<Status> _Status;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<Strategy> Strategy
            {
                get
                {
                    if ((_Strategy == null))
                    {
                        _Strategy = base.CreateObjectSet<Strategy>("Strategy");
                    }
                    return _Strategy;
                }
            }
            private ObjectSet<Strategy> _Strategy;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<sysdiagrams> sysdiagrams
            {
                get
                {
                    if ((_sysdiagrams == null))
                    {
                        _sysdiagrams = base.CreateObjectSet<sysdiagrams>("sysdiagrams");
                    }
                    return _sysdiagrams;
                }
            }
            private ObjectSet<sysdiagrams> _sysdiagrams;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<Terminal> Terminal
            {
                get
                {
                    if ((_Terminal == null))
                    {
                        _Terminal = base.CreateObjectSet<Terminal>("Terminal");
                    }
                    return _Terminal;
                }
            }
            private ObjectSet<Terminal> _Terminal;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<TerminalType> TerminalType
            {
                get
                {
                    if ((_TerminalType == null))
                    {
                        _TerminalType = base.CreateObjectSet<TerminalType>("TerminalType");
                    }
                    return _TerminalType;
                }
            }
            private ObjectSet<TerminalType> _TerminalType;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<Ticker> Ticker
            {
                get
                {
                    if ((_Ticker == null))
                    {
                        _Ticker = base.CreateObjectSet<Ticker>("Ticker");
                    }
                    return _Ticker;
                }
            }
            private ObjectSet<Ticker> _Ticker;
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            public ObjectSet<Transaction> Transaction
            {
                get
                {
                    if ((_Transaction == null))
                    {
                        _Transaction = base.CreateObjectSet<Transaction>("Transaction");
                    }
                    return _Transaction;
                }
            }
            private ObjectSet<Transaction> _Transaction;
    
            #endregion
            #region AddTo Methods
    
            /// <summary>
            /// Deprecated Method for adding a new object to the Broker EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToBroker(Broker broker)
            {
                base.AddObject("Broker", broker);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the Client EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToClient(Client client)
            {
                base.AddObject("Client", client);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the ClientTicker EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToClientTicker(ClientTicker clientTicker)
            {
                base.AddObject("ClientTicker", clientTicker);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the Currency EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToCurrency(Currency currency)
            {
                base.AddObject("Currency", currency);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the Level EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToLevel(Level level)
            {
                base.AddObject("Level", level);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the Risk EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToRisk(Risk risk)
            {
                base.AddObject("Risk", risk);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the Status EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToStatus(Status status)
            {
                base.AddObject("Status", status);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the Strategy EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToStrategy(Strategy strategy)
            {
                base.AddObject("Strategy", strategy);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the sysdiagrams EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddTosysdiagrams(sysdiagrams sysdiagrams)
            {
                base.AddObject("sysdiagrams", sysdiagrams);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the Terminal EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToTerminal(Terminal terminal)
            {
                base.AddObject("Terminal", terminal);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the TerminalType EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToTerminalType(TerminalType terminalType)
            {
                base.AddObject("TerminalType", terminalType);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the Ticker EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToTicker(Ticker ticker)
            {
                base.AddObject("Ticker", ticker);
            }
    
            /// <summary>
            /// Deprecated Method for adding a new object to the Transaction EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
            /// </summary>
            public void AddToTransaction(Transaction transaction)
            {
                base.AddObject("Transaction", transaction);
            }
    
            #endregion
        }
    
    
        #endregion
    
        #region Entities
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmEntityTypeAttribute(NamespaceName="Main_dataBaseModel", Name="Broker")]
        [Serializable()]
        [DataContractAttribute(IsReference=true)]
        public partial class Broker : EntityObject
        {
            #region Factory Method
    
            /// <summary>
            /// Create a new Broker object.
            /// </summary>
            /// <param name="id">Initial value of the ID property.</param>
            public static Broker CreateBroker(global::System.Guid id)
            {
                Broker broker = new Broker();
                broker.ID = id;
                return broker;
            }
    
            #endregion
            #region Primitive Properties
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
            [DataMemberAttribute()]
            public global::System.Guid ID
            {
                get
                {
                    return _ID;
                }
                set
                {
                    if (_ID != value)
                    {
                        OnIDChanging(value);
                        ReportPropertyChanging("ID");
                        _ID = StructuralObject.SetValidValue(value);
                        ReportPropertyChanged("ID");
                        OnIDChanged();
                    }
                }
            }
            private global::System.Guid _ID;
            partial void OnIDChanging(global::System.Guid value);
            partial void OnIDChanged();
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
            [DataMemberAttribute()]
            public global::System.String Name
            {
                get
                {
                    return _Name;
                }
                set
                {
                    OnNameChanging(value);
                    ReportPropertyChanging("Name");
                    _Name = StructuralObject.SetValidValue(value, true);
                    ReportPropertyChanged("Name");
                    OnNameChanged();
                }
            }
            private global::System.String _Name;
            partial void OnNameChanging(global::System.String value);
            partial void OnNameChanged();
    
            #endregion
    
            #region Navigation Properties
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [XmlIgnoreAttribute()]
            [SoapIgnoreAttribute()]
            [DataMemberAttribute()]
            [EdmRelationshipNavigationPropertyAttribute("Main_dataBaseModel", "FK_Terminals_Brokers", "Terminal")]
            public EntityCollection<Terminal> Terminal
            {
                get
                {
                    return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Terminal>("Main_dataBaseModel.FK_Terminals_Brokers", "Terminal");
                }
                set
                {
                    if ((value != null))
                    {
                        ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Terminal>("Main_dataBaseModel.FK_Terminals_Brokers", "Terminal", value);
                    }
                }
            }
    
            #endregion
        }
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmEntityTypeAttribute(NamespaceName="Main_dataBaseModel", Name="Client")]
        [Serializable()]
        [DataContractAttribute(IsReference=true)]
        public partial class Client : EntityObject
        {
            #region Factory Method
    
            /// <summary>
            /// Create a new Client object.
            /// </summary>
            /// <param name="id">Initial value of the ID property.</param>
            public static Client CreateClient(global::System.Guid id)
            {
                Client client = new Client();
                client.ID = id;
                return client;
            }
    
            #endregion
            #region Primitive Properties
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
            [DataMemberAttribute()]
            public global::System.Guid ID
            {
                get
                {
                    return _ID;
                }
                set
                {
                    if (_ID != value)
                    {
                        OnIDChanging(value);
                        ReportPropertyChanging("ID");
                        _ID = StructuralObject.SetValidValue(value);
                        ReportPropertyChanged("ID");
                        OnIDChanged();
                    }
                }
            }
            private global::System.Guid _ID;
            partial void OnIDChanging(global::System.Guid value);
            partial void OnIDChanged();
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
            [DataMemberAttribute()]
            public global::System.String Name
            {
                get
                {
                    return _Name;
                }
                set
                {
                    OnNameChanging(value);
                    ReportPropertyChanging("Name");
                    _Name = StructuralObject.SetValidValue(value, true);
                    ReportPropertyChanged("Name");
                    OnNameChanged();
                }
            }
            private global::System.String _Name;
            partial void OnNameChanging(global::System.String value);
            partial void OnNameChanged();
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
            [DataMemberAttribute()]
            public global::System.String Account
            {
                get
                {
                    return _Account;
                }
                set
                {
                    OnAccountChanging(value);
                    ReportPropertyChanging("Account");
                    _Account = StructuralObject.SetValidValue(value, true);
                    ReportPropertyChanged("Account");
                    OnAccountChanged();
                }
            }
            private global::System.String _Account;
            partial void OnAccountChanging(global::System.String value);
            partial void OnAccountChanged();
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
            [DataMemberAttribute()]
            public global::System.String Comment
            {
                get
                {
                    return _Comment;
                }
                set
                {
                    OnCommentChanging(value);
                    ReportPropertyChanging("Comment");
                    _Comment = StructuralObject.SetValidValue(value, true);
                    ReportPropertyChanged("Comment");
                    OnCommentChanged();
                }
            }
            private global::System.String _Comment;
            partial void OnCommentChanging(global::System.String value);
            partial void OnCommentChanged();
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
            [DataMemberAttribute()]
            public Nullable<global::System.Single> Assets
            {
                get
                {
                    return _Assets;
                }
                set
                {
                    OnAssetsChanging(value);
                    ReportPropertyChanging("Assets");
                    _Assets = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("Assets");
                    OnAssetsChanged();
                }
            }
            private Nullable<global::System.Single> _Assets;
            partial void OnAssetsChanging(Nullable<global::System.Single> value);
            partial void OnAssetsChanged();
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
            [DataMemberAttribute()]
            public global::System.String Virtual_Assets
            {
                get
                {
                    return _Virtual_Assets;
                }
                set
                {
                    OnVirtual_AssetsChanging(value);
                    ReportPropertyChanging("Virtual_Assets");
                    _Virtual_Assets = StructuralObject.SetValidValue(value, true);
                    ReportPropertyChanged("Virtual_Assets");
                    OnVirtual_AssetsChanged();
                }
            }
            private global::System.String _Virtual_Assets;
            partial void OnVirtual_AssetsChanging(global::System.String value);
            partial void OnVirtual_AssetsChanged();
    
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttri
    

2 个答案:

答案 0 :(得分:2)

你试过吗?

context.Levels.Add(z);
context.SaveChanges();

如果这不起作用,请围绕上述操作包装一个异常处理程序,并捕获抛出的异常。

所以完整的代码将是:

using(var context = new Main_dataBaseEntities())
{
  try
  { 
      var z = new Level {Path = "zPath"};
      context.Levels.Add(z);
      context.SaveChanges();
  }
  catch(Exception ex)
  {
      //View or handle exception here
  }      
}

您应该在生产代码中使用更具体的异常替换上面的Exception,但这样就足以查看导致问题的原因。

答案 1 :(得分:0)

savechanges方法返回处于已添加,已修改或已删除状态的对象数,尝试调试并查看返回值的内容以及是否抛出任何异常。我个人更喜欢数据库第一种方法。

int num = context.SaveChanges();