如何将表数据库中的列映射到属性和与Fields的关系

时间:2016-10-21 10:03:12

标签: sensenet

我正在研究Sensenet Framework并在我的计算机上成功安装,现在我正在基于这个框架开发我们的网站。
我阅读了wiki上的文档并理解了数据库< - >之间的关系。属性< - >字段< - >查看(您可以在此链接中查看图片:http://wiki.sensenet.com/Field_-_for_Developers)。
假设,如果我在Sensenet的数据库中添加了一个新表,并且想要将该表中的所有数据显示到我们的页面,但是我不知道该模型如何开发流程:Database< =>属性< =>字段< =>视图。你能说出帮助我的步骤吗?

1 个答案:

答案 0 :(得分:2)

请考虑将数据存储在SenseNet 内容存储库中,而不是将自定义表保留在数据库中。使用常规内容项目更容易,并且您将拥有回购提供的所有功能 - 例如索引,权限,当然还有现有的UI。为此,您必须执行以下步骤:

    对于您现有数据库中的每种实体类型,在SenseNet中
  1. 定义内容类型(在下面的示例中,这是 Car 类型)。
  2. 在内容存储库中创建容器,您要在其中放置内容(在这种情况下,这是默认网站下的汽车自定义列表)。
  3. 使用SenseNet Client library创建命令行工具,将现有数据迁移到内容存储库。
  4. 要详细查看示例,请查看此文章:

    该示例的核心实际上是几行代码,实际上将内容项保存到内容库中(通过REST API):

    using (var conn = new SqlConnection(ConnectionString))
    {
        await conn.OpenAsync();
    
        using (var command = new SqlCommand("SELECT * FROM Cars", conn))
        {
            using (var reader = await command.ExecuteReaderAsync())
            {
                while (await reader.ReadAsync())
                {
                    var id = reader.GetInt32(0);
                    var make = reader.GetString(1);
                    var model = reader.GetString(2);
                    var price = reader.GetInt32(3);
    
                    // Build a new content in memory and fill custom metadata fields. No need to create
                    // strongly typed objects here as the client Content is a dynamic type.
                    // Parent path is a Content Repository path, e.g. "/Root/Sites/Default_Site/Cars"
                    dynamic car = Content.CreateNew(ParentPath, "Car", "Car-" + id);
                    car.Make = make;
                    car.Model = model;
                    car.Price = price;
    
                    // save it through the HTTP REST API
                    await car.SaveAsync();
    
                    Console.WriteLine("Car-" + id + " saved.");
                }
            }
        }
    }
    
相关问题