【问题标题】:Import CSV/Excel file data into 2 tables (sql server 2012) using Fluent Nhibernate c#使用 Fluent Nhibernate c# 将 CSV/Excel 文件数据导入 2 个表(sql server 2012)
【发布时间】:2014-04-15 05:41:00
【问题描述】:

嗨,
我有一个 csv/excel 文件,想使用 C# Fluent Nhibernate 存储库模式将 excel/csv 文件中的数据插入我的 sql server 数据库的 2 个表中。

我的两张桌子是:

table1 (Id, FieldName)
table2 (Id, table1Id, Name, ...)

table1 的主键在 table2 中充当外键。

在这 2 个表格中插入的全部数据都可以在单个 excel/csv 文件中找到。那么,是否可以使用 C# Fluent Nhibernate 来完成这个过程?如果是这样,请任何人给我一个想法?

谢谢, sampath

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate


    【解决方案1】:

    是的,这绝对是可能的。这是一个非常基本的示例,说明您将如何执行此操作。

    伪代码:

    实体

    class table1
    {
        int id;
        List<table2> table2s;
    }
    
    class table2
    {
        int id;
        table1 instance;
        string name;
    }
    

    映射

    public class table1Map: ClassMap<table1>
    {
      public table1Map()
      {
        Table("table1");
        Id(x => x.Id);
        HasMany(x => x.table2s).Cascade.All();
      }
    }
    
    public class table2Map: ClassMap<table2>
    {
      public table2Map()
      {
        Table("table2");
        Id(x => x.Id);
        References(x => x.instance);
      }
    }
    

    用法

    using (var session = new  Configuration().Configure().BuildSessionFactory().OpenSession())  
    {  
        table1 t1 = new table1();
        t1.table2s.Add(new table2());
    
        using (ITransaction transaction = session.BeginTransaction())  
        {  
            session.Save(t1);
            transaction.Commit();  
        }  
    }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多