我有一张表create table XMLFORM (
   ID                   int                  identity,
   FORMXML              xml                  not null,
   constraint PK_XMLFORM primary key (ID)
)想使用LINQ保存XML[AcceptVerbs(HttpVerbs.Post),ValidateInput(false)]
        public ActionResult New(string xmlContent)
        {
            xmlContent = Server.UrlDecode(xmlContent);
            if (!string.IsNullOrEmpty(xmlContent))
            {
                //if (!CheckXML(xmlContent, out strError)) //服务器端检测,如果用了ajax检测,就没必要了
                //{
                //    ModelState.AddModelError("_FORM",strError);  
                //    return View();
                //}
                XMLFormDataContext db = new XMLFormDataContext();
                TransactionOptions opt = new TransactionOptions();
                opt.IsolationLevel = IsolationLevel.Serializable;
                using (TransactionScope tran = new TransactionScope(TransactionScopeOption.RequiresNew, opt))
                {
                    XMLFORM f = new XMLFORM();
                    try
                    {
                        f.FORMXML = XElement.Parse(xmlContent);
                        db.XMLFORMs.InsertOnSubmit(f);
                        db.SubmitChanges();
                        var id = db.XMLFORMs.Max(p => p.ID);
                        TempData["resultMessage"] = "Success";
                        TempData["id"] = (int)id;
                        return RedirectToAction("Result");
                    }
                    catch
                    {
                        TempData["resultMessage"] = "failure";
                        ModelState.AddModelError("_FORM", "envalid xml format");
                        return View();
                    }
                }
            }
            else
                return View();
        }问题是这里
{code=C#]
 f.FORMXML = XElement.Parse(xmlContent);
                        db.XMLFORMs.InsertOnSubmit(f);
                        db.SubmitChanges();
[/code]
保存没报错,select ID也返回值了,但是用sqlserver management看是NULL,NULL
程序里也读不出值,请教高手,如何我用linq保存XML?

解决方案 »

  1.   

    这么残缺的代码没法调试,只能推测而已。既然var id = db.XMLFORMs.Max(p => p.ID)能有返回,说明问题应该在db.SubmitChanges()上了。
    没看到XMLFormDataContext类的内容,不好说啊。
      

  2.   


    #pragma warning disable 1591
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:2.0.50727.3074
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------namespace MVCMembership.Models
    {
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Data;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Linq;
    using System.Linq.Expressions;
    using System.ComponentModel;
    using System;


    [System.Data.Linq.Mapping.DatabaseAttribute(Name="Test")]
    public partial class XMLFormDataContext : System.Data.Linq.DataContext
    {

    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

        #region Extensibility Method Definitions
        partial void OnCreated();
        partial void InsertXMLFORM(XMLFORM instance);
        partial void UpdateXMLFORM(XMLFORM instance);
        partial void DeleteXMLFORM(XMLFORM instance);
        #endregion

    public XMLFormDataContext() : 
    base(global::System.Configuration.ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString, mappingSource)
    {
    OnCreated();
    }

    public XMLFormDataContext(string connection) : 
    base(connection, mappingSource)
    {
    OnCreated();
    }

    public XMLFormDataContext(System.Data.IDbConnection connection) : 
    base(connection, mappingSource)
    {
    OnCreated();
    }

    public XMLFormDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
    base(connection, mappingSource)
    {
    OnCreated();
    }

    public XMLFormDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
    base(connection, mappingSource)
    {
    OnCreated();
    }

    public System.Data.Linq.Table<XMLFORM> XMLFORMs
    {
    get
    {
    return this.GetTable<XMLFORM>();
    }
    }
    }

    [Table(Name="dbo.XMLFORM")]
    public partial class XMLFORM : INotifyPropertyChanging, INotifyPropertyChanged
    {

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _ID;

    private System.Xml.Linq.XElement _FORMXML;

        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnIDChanging(int value);
        partial void OnIDChanged();
        partial void OnFORMXMLChanging(System.Xml.Linq.XElement value);
        partial void OnFORMXMLChanged();
        #endregion

    public XMLFORM()
    {
    OnCreated();
    }

    [Column(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int ID
    {
    get
    {
    return this._ID;
    }
    set
    {
    if ((this._ID != value))
    {
    this.OnIDChanging(value);
    this.SendPropertyChanging();
    this._ID = value;
    this.SendPropertyChanged("ID");
    this.OnIDChanged();
    }
    }
    }

    [Column(Storage="_FORMXML", DbType="Xml NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
    public System.Xml.Linq.XElement FORMXML
    {
    get
    {
    return this._FORMXML;
    }
    set
    {
    if ((this._FORMXML != value))
    {
    this.OnFORMXMLChanging(value);
    this.SendPropertyChanging();
    this._FORMXML = value;
    this.SendPropertyChanged("FORMXML");
    this.OnFORMXMLChanged();
    }
    }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
    if ((this.PropertyChanging != null))
    {
    this.PropertyChanging(this, emptyChangingEventArgs);
    }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
    if ((this.PropertyChanged != null))
    {
    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    }
    }
    }
    #pragma warning restore 1591
      

  3.   

    那个返回的ID有没有增长,如果你插入成功的话,每次打开程序插入,ID都会增长的,否则每次重新打开程序插入,ID都是从1开始。
      

  4.   

    晕死,我没注意到你是放在事务里执行的,你的事务没有提交,所以最后程序结束的时候回滚了。那个tran没有提交啊,using块结束后就回滚了。