暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

CodeFirst 模式-GBASE南大通用

原创 淮海路小佩奇 2023-12-26
113

通过编写代码直接操作数据表!需要在 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;

}

}

}

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论