GBASE南大通用-CodeFirst 模式

本文介绍了如何在C#中通过EntityFrameworkCodeFirst实现对GBASE数据库的操作,包括配置连接串和执行插入与查询数据的代码示例。
摘要由CSDN通过智能技术生成

通过编写代码直接操作数据表!需要在 GBASE南大通用App.config 中配置相应的连接串:

<connectionStrings>

<add name="BloggingContext"

connectionString="server=192.168.5.4;User

Id=sysdba;password=1;Initial Catalog=BlogTest;

Persist Security Info=True;"

providerName="GBase.Data.GBaseClient"

/>

</connectionStrings>

C#代码示例:

namespace EF_codefirst

{

public class Blog

{

public int BlogId { get; set; }

public string Name { get; set; }

public virtual List<Post> Posts { get; set; }

}

public class Post

{

public int PostId { get; set; }

public string Title { get; set; }

public string Content { get; set; }

public int BlogId { get; set; }

public virtual Blog Blog { get; set; }

}

public class BloggingContext : DbContext

{

public DbSet<Blog> Blogs { get; set; }

public DbSet<Post> Posts { get; set; }

}

class Program

{

static void Main(string[] args)

{

InsertData();

QueryData();

}

/// <summary>

/// 插入数据

/// </summary>

public static void InsertData()

{

try

{

using (var db = new BloggingContext())

{

//Create and save a new Blog

Console.Write("Enter a name for a new Blog:");

var name = Console.ReadLine();

var blog = new Blog { Name = name };

db.Blogs.Add(blog);

db.SaveChanges();

}

}

catch (System.Exception ex)

{

throw ex.InnerException;

}

QueryData();

}

/// <summary>

/// 查询数据

/// </summary>

public static void QueryData()

{

try

{

using (var db = new BloggingContext())

{

//Display all Blogs from the DB

var query = from b in db.Blogs

orderby b.Name

select b;

Console.WriteLine("All blogs in the database:");

foreach (var item in query)

{

Console.WriteLine(item.Name);

}

Console.WriteLine("Press any key to exit...");

Console.ReadKey();

}

}

catch (System.Exception ex)

{

throw;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值