Entity Framework 对新数据库使用 Code First
来源:互联网 发布:nz挂机软件 编辑:程序博客网 时间:2024/06/14 20:08
( 更多视频选项 — 包括下载)
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; }
}
using System.Data.Entity;
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace CodeFirstNewDatabaseSample
{
class Program
{
static void Main(string[] args)
{
}
}
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)
{
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();
// Display all Blogs from the database
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();
}
}
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public virtual List<Post> Posts { get; set; }
}
namespace CodeFirstNewDatabaseSample.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class AddUrl : DbMigration
{
public override void Up()
{
AddColumn("dbo.Blogs", "Url", c => c.String());
}
public override void Down()
{
DropColumn("dbo.Blogs", "Url");
}
}
}
public class User
{
public string Username { get; set; }
public string DisplayName { get; set; }
}
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<User> Users { get; set; }
}
using System.ComponentModel.DataAnnotations;
public class User
{
[Key]
public string Username { get; set; }
public string DisplayName { get; set; }
}
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.Property(u => u.DisplayName)
.HasColumnName("display_name");
}
}
0 0
- Entity Framework 对新数据库使用 Code First
- Entity Framework使用建模之Code First 多对多
- Entity Framework(code first)数据库初始化
- Entity Framework Code First数据库自动更新
- Entity Framework Code first
- Code First :使用Entity. Framework编程(7)
- Code First :使用Entity. Framework编程(7)
- Code First :使用Entity. Framework编程(3)
- Code First :使用Entity. Framework编程(1)
- Code First :使用Entity. Framework编程(2)
- Entity Framework Code First使用DbContext查询
- Entity Framework Code First使用DbContext查询
- Entity Framework 6 Code First新特性:支持存储过程
- Entity Framework 6.1-Code First
- Entity Framework 6.1-Code First
- Entity Framework Code First数据库连接
- Entity Framework之Code First
- Entity Framework Code First数据库连接
- 铭瑄 R9 270 显卡(1GB 版本)使用记
- C#使用GET、POST请求获取结果
- 面向对象
- C 中 & | ~ ^ 与 或 反 异或 的用法
- pip安装相关
- Entity Framework 对新数据库使用 Code First
- ExtJs4学习(十二)layout布局
- c语言应用
- can bind a LONG value only for insert into a LONG column
- 网上兼职是真的吗?淘宝兼职是真的吗?
- Linux 下 TCP C代码
- PHP构造验证码
- 【BZOJ1202】【HNOI2005】狡猾的商人 并查集
- ECShop后台站点地图关于 Deprecated: Assigning the return value of new by reference is deprecated的错误的解决办法