RavenDB中的批量插入

本文关键字:插入 RavenDB | 更新日期: 2023-09-27 18:23:48

我想在RaverDB数据库中大容量插入从XML文件反序列化的一些行。

我写的代码是以下

public RavenDBImport(PROJECTSRow dsrlizedObject)
{
    IDocumentStore docuStore = ConnectToDB();

    using (var session = docuStore.OpenSession())
    {
        var existingDoc = session.Load<PROJECTSRow>(dsrlizedObject.IC_NAME);
        if (existingDoc == null)
        {
            ERROR
            PROJECTSRow row = new PROJECTSRow()
                {
                    IC_NAME = dsrlizedObject.IC_NAME,
                    SERIAL_NUMBER = dsrlizedObject.SERIAL_NUMBER,
                    TOTAL_COST = dsrlizedObject.TOTAL_COST,
                    ADMINISTERING_IC = dsrlizedObject.ADMINISTERING_IC,
                    FUNDING_MECHANISM = dsrlizedObject.FUNDING_MECHANISM
                };
            session.Store(row);
        }
        else
        {
            Console.WriteLine("*******************************************");
            Console.Write("A Document with the same IC_NAME alredy exists in the database.");
            Console.WriteLine("*******************************************");
        }
        session.SaveChanges();
    }
}

#region ConnectToDB
// ConnectToDB - Create Connection with the DB - TestDB
public static IDocumentStore ConnectToDB()
    {
        var documentStore = new Raven.Client.Document.DocumentStore { Url = "http://hyperpc:8080/", DefaultDatabase = "TestDB" };
        documentStore.Initialize();
        return documentStore;
    }

它被称为。。。

    foreach (PROJECTSRow r in SBIRSTTRSelectedRows)
    {
        // ImportToDB Call
        new RavenDBImport(r);
    }

问题是,数据库中可能已经存在IC_NAME与我要导入的公司(例如Company1 LLC和Company1 LTD.)不完全相同但相似的现有公司。目前,它会检查完全相同的IC_NAME。我可以做些什么来识别这些公司,然后手动决定是否要导入文档?

感谢

BTW该类为以下

namespace XML2RavenDBConverter
{
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class PROJECTS
    {
        private PROJECTSRow[] itemsField;
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("row", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public PROJECTSRow[] Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }
    }
    [Serializable]
    public class PROJECTSRow
    {
        [XmlElement("IC_NAME")]
        public string IC_NAME { get; set; }
        [XmlElement("SERIAL_NUMBER")]
        public string SERIAL_NUMBER { get; set; }
        [XmlElement("TOTAL_COST")]
        public string TOTAL_COST { get; set; }
        [XmlElement("ADMINISTERING_IC")]
        public string ADMINISTERING_IC { get; set; }
        [XmlElement("FUNDING_MECHANISM")]
        public string FUNDING_MECHANISM { get; set; }
        //
        // more to follow
        //

        [XmlElement()]
        private rowPISPI[][] PISField;
        [XmlElement("PROJECT_TERMSX")]
        private rowPROJECT_TERMSXTERM[][] PROJECT_TERMSXField;
    }

        [Serializable]
        public class rowPISPI
        {
            [XmlElement("PI_NAME")]
            public string PI_NAME {get; set; }
            [XmlElement("PI_ID")]
            public string PI_ID {get; set; }
        }
        [Serializable]
        public class rowPROJECT_TERMSXTERM
        {
            [XmlElement("TERM")]
            public string valueField { get; set; }
        }
}

RavenDB中的批量插入

您可能不应该一次追加一条记录。RavenDB有一个大容量插入功能,这将使事情变得更容易、更快。看见http://ravendb.net/docs/2.0/client-api/advanced/bulk-inserts